The Student Room Group

code help

Hello,
I was making a game which repeats until the user wants to and at the end of each game the name and score of the winner is stored into the file: "winners.txt". However the file restarts each time that the user repeats the game. This means that even if the users play the game 5 times the name and score of only the last winner is being stored into the file. If you could please tell me why and how can i solve it please..
Thanks.
What language is this?
The problem lies in this line of code:


myFile = open("winners.txt", "w")



The parameter "w" effectively overwrites "winners.txt" if it already exists. To fix this, you need to change "w" to "a" (which is shorthand for append), this will add to the file, rather than completely overwrite it. "a" will also create a new file if the file name "winners.txt" does not exist. I hope this helps.

More information on the parameter values can be read here: https://www.guru99.com/reading-and-writing-files-in-python.html
Original post by Charlie101998
What language is this?

python
Original post by Strange5050
The problem lies in this line of code:


myFile = open("winners.txt", "w")



The parameter "w" effectively overwrites "winners.txt" if it already exists. To fix this, you need to change "w" to "a" (which is shorthand for append), this will add to the file, rather than completely overwrite it. "a" will also create a new file if the file name "winners.txt" does not exist. I hope this helps.

More information on the parameter values can be read here: https://www.guru99.com/reading-and-writing-files-in-python.html

thanks a lot...
Original post by Strange5050
The problem lies in this line of code:


myFile = open("winners.txt", "w")



The parameter "w" effectively overwrites "winners.txt" if it already exists. To fix this, you need to change "w" to "a" (which is shorthand for append), this will add to the file, rather than completely overwrite it. "a" will also create a new file if the file name "winners.txt" does not exist. I hope this helps.

More information on the parameter values can be read here: https://www.guru99.com/reading-and-writing-files-in-python.html

thanks a lot...
Original post by Strange5050
The problem lies in this line of code:


myFile = open("winners.txt", "w")



The parameter "w" effectively overwrites "winners.txt" if it already exists. To fix this, you need to change "w" to "a" (which is shorthand for append), this will add to the file, rather than completely overwrite it. "a" will also create a new file if the file name "winners.txt" does not exist. I hope this helps.

More information on the parameter values can be read here: https://www.guru99.com/reading-and-writing-files-in-python.html

If you could please help me again:
My game is such that the users have to register themselves and they can replay the game. But when they are replaying they would have to login and if a new user has joined they have to register themselves.
But the problem is that once a user registers himself, his details are stored in the file "registrationp1.txt" for player one and "registrationp2.txt" for player 2 and their username and password are also stored in "usernamep1.txt" for player one and "usernamep2.txt" for player two in the form of (username+password). E.g ammsi03Hello123

The problem that is now arising is that when the game replays if instead of the two old users/players, a new user wants to come in, he would have to register and if he registers as player 1, his details would be stored in usernamep1.txt or registerationp2.txt. This can be solved as i can then make use of "a" append instead of "w"write to add on to the same file but then that would produce something like this into the file: ammsi03Hello123innsi05Hello126 where the username of player 1 is ammsi03 and its password is Hello123 and the username of player 2 is inssi05 and its password is Hello126. But during login where i have to check the username+password that the user/player has entered with that in the file, i would have to make use of a specific way to only read this bit (in bold) from the file.E.g from usernamesp1.txt: ammsi03Hello123innsi05Hello126


Is there any possible way?
Thanks again..
Original post by sidick angel
If you could please help me again:
My game is such that the users have to register themselves and they can replay the game. But when they are replaying they would have to login and if a new user has joined they have to register themselves.
But the problem is that once a user registers himself, his details are stored in the file "registrationp1.txt" for player one and "registrationp2.txt" for player 2 and their username and password are also stored in "usernamep1.txt" for player one and "usernamep2.txt" for player two in the form of (username+password). E.g ammsi03Hello123

The problem that is now arising is that when the game replays if instead of the two old users/players, a new user wants to come in, he would have to register and if he registers as player 1, his details would be stored in usernamep1.txt or registerationp2.txt. This can be solved as i can then make use of "a" append instead of "w"write to add on to the same file but then that would produce something like this into the file: ammsi03Hello123innsi05Hello126 where the username of player 1 is ammsi03 and its password is Hello123 and the username of player 2 is inssi05 and its password is Hello126. But during login where i have to check the username+password that the user/player has entered with that in the file, i would have to make use of a specific way to only read this bit (in bold) from the file.E.g from usernamesp1.txt: ammsi03Hello123innsi05Hello126


Is there any possible way?
Thanks again..

One possible workaround for this would be to manually 'append' an additional space character (" ") onto the end of the username and passwords so that they'd be separated. I don't personally know the viability of this method myself but is a suggestion you could experiment with.

I'd be more inclined to change the format of the file you're using to store that information, from a .txt to a .csv . A .csv file stands for 'comma-separated values' and maybe much more suitable for what you need. You can read more about Python's built-in .csv related code but here: https://docs.python.org/3/library/csv.html and you'll need to 'import csv' into your project.

In essence here's a good example from the link above (for the process of storing the values):


with open('names.csv', 'a') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})



Have a read in the documentation, and have a look online around Python and .csv files and you should find this to be a much better solution. In essence it's simply a table stored in a file.

Hope this helps.
(edited 4 years ago)
Original post by sidick angel
If you could please help me again:
My game is such that the users have to register themselves and they can replay the game. But when they are replaying they would have to login and if a new user has joined they have to register themselves.
But the problem is that once a user registers himself, his details are stored in the file "registrationp1.txt" for player one and "registrationp2.txt" for player 2 and their username and password are also stored in "usernamep1.txt" for player one and "usernamep2.txt" for player two in the form of (username+password). E.g ammsi03Hello123

The problem that is now arising is that when the game replays if instead of the two old users/players, a new user wants to come in, he would have to register and if he registers as player 1, his details would be stored in usernamep1.txt or registerationp2.txt. This can be solved as i can then make use of "a" append instead of "w"write to add on to the same file but then that would produce something like this into the file: ammsi03Hello123innsi05Hello126 where the username of player 1 is ammsi03 and its password is Hello123 and the username of player 2 is inssi05 and its password is Hello126. But during login where i have to check the username+password that the user/player has entered with that in the file, i would have to make use of a specific way to only read this bit (in bold) from the file.E.g from usernamesp1.txt: ammsi03Hello123innsi05Hello126


Is there any possible way?
Thanks again..


You will have to look this up as I can't remeber the specifics but wht not save usernames and passwords with commas separating them?

You can the read a line and do something along the lines of:

ArrayOfUsernames = line.split (",")
Username1= ArrayOfUsernames[0]

Etc
Thanks for all your replies..
Original post by sidick angel
Thanks for all your replies..

I want some more help please..
If anyone could please tell me how to sort out this array..
thanks a lot..
Original post by sidick angel
I want some more help please..
If anyone could please tell me how to sort out this array..
thanks a lot..

I typed "Python sort array" into Google and it gave me
https://www.programiz.com/python-programming/methods/list/sort

So basically

a = ['fg','yg','ye']

sorted = a.sort()

Or you could implement your own sorting function. For OCR GCSE you need to know bubble, merge and insertion. Google can help you find implementations or algorithms - whatever you need.

Have fun!

Quick Reply

Latest

Trending

Trending