sidick angel
Badges: 9
Rep:
?
#1
Report Thread starter 1 year ago
#1
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.
0
reply
sidick angel
Badges: 9
Rep:
?
#2
Report Thread starter 1 year ago
#2
this is my code
0
reply
Charlie101998
Badges: 18
Rep:
?
#3
Report 1 year ago
#3
What language is this?
0
reply
Strange5050
Badges: 17
Rep:
?
#4
Report 1 year ago
#4
The problem lies in this line of code:
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-w...in-python.html
1
reply
sidick angel
Badges: 9
Rep:
?
#5
Report Thread starter 1 year ago
#5
(Original post by Charlie101998)
What language is this?
python
0
reply
sidick angel
Badges: 9
Rep:
?
#6
Report Thread starter 1 year ago
#6
(Original post by Strange5050)
The problem lies in this line of code:
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-w...in-python.html
thanks a lot...
0
reply
sidick angel
Badges: 9
Rep:
?
#7
Report Thread starter 1 year ago
#7
(Original post by Strange5050)
The problem lies in this line of code:
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-w...in-python.html
thanks a lot...
0
reply
sidick angel
Badges: 9
Rep:
?
#8
Report Thread starter 1 year ago
#8
(Original post by Strange5050)
The problem lies in this line of code:
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-w...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..
0
reply
Strange5050
Badges: 17
Rep:
?
#9
Report 1 year ago
#9
(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):

Code:
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.
Last edited by Strange5050; 1 year ago
0
reply
username1799249
Badges: 19
Rep:
?
#10
Report 1 year ago
#10
(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
0
reply
sidick angel
Badges: 9
Rep:
?
#11
Report Thread starter 1 year ago
#11
Thanks for all your replies..
0
reply
sidick angel
Badges: 9
Rep:
?
#12
Report Thread starter 1 year ago
#12
(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..
Attached files
0
reply
username1799249
Badges: 19
Rep:
?
#13
Report 1 year ago
#13
(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-pro...hods/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!
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Feeling behind at school/college? What is the best thing your teachers could to help you catch up?

Extra compulsory independent learning activities (eg, homework tasks) (8)
5.59%
Run extra compulsory lessons or workshops (24)
16.78%
Focus on making the normal lesson time with them as high quality as possible (24)
16.78%
Focus on making the normal learning resources as high quality/accessible as possible (21)
14.69%
Provide extra optional activities, lessons and/or workshops (41)
28.67%
Assess students, decide who needs extra support and focus on these students (25)
17.48%

Watched Threads

View All