Adding integers and strings into a text file
Hey everyone,
In my game quiz, I need to store the user and their scores in a text file. But, the problem is:
- I tried storing the data but I don't know how to output because an error comes up saying there are not enough data in the text file
- the program does not update previous scores if the same user plays
I need help with storing scores and user and printing the top 5.
Thanks
Use a database, it’s more secure and easier to use. To use the database you first have to import it by doing: from replit import db
Then you can use the db just like a dictionary except the name of the dictionary is db
@IMayBeMe Thanks, I've imported it, now what do I do? Sorry our school doesn't use repl we use idle, that's why I don't know how to use the database.
Treat db
as a dict. So that means you can do something like:
db['num'] = 5
@FatimaNadeem1
@Coder100 can you specifically tell me where to use that so like how do I use the database to store the score in a text file?
db['total'] = score
f = open("leaderboard.txt","a+")
f.write(str(total)+ " -" + user_names)
f.write("\n")
@FatimaNadeem1 you don’t the database is an alternative to a text file
@IMayBeMe Oh, so can you show me what the code will look like at the end for the leader board?
@FatimaNadeem1 no, I was just adding on to the replit db suggestion
replit db and file-based storage are mutually exclusive. You can choose one or the other.
@Coder100 Oh, i would like to use the file-based storage because that's what we are told but i'm kind of confused on why it's not working.
@FatimaNadeem1 what isn't working
You shouldn't just let other people do all the work ;) @FatimaNadeem1
@QuickV i've done all of the code on my own, i just dunno how to add the leaderboard
@Coder100 from line 128 to 141, i don't know how to add scores and names onto the text file in a list and put them in order and then print the top 5
ah ok... that makes sense. Once you have the leaderboard, you should probably go over it and try to understand how it works, so you can make your own in the future :) @FatimaNadeem1
@QuickV I will but people are still not telling me how to do the leader board :(
@FatimaNadeem1 seems to me you know that steps to solve the issue. Programming is about problem solving so figure out each of those steps and piece them together
@IMayBeMe I've been doing it for a long time, that's why I came here to ask for help because I can't figure it out.
Maybe nobody knows how .-. Maybe try googling it? @FatimaNadeem1
You might find it on google tho :P @FatimaNadeem1
@QuickV Any specific websites you know that may help?
I don't really know any... I'm not that familiar/good with Python, but I do know that @CodingCactus made a python overview sort of thing... You can find it here if you want.
@QuickV There's no info about text files in there
hmm... maybe this or this... Sorry if none of those are useful lol... I don't really know anything about Python (and I haven't used text files for anything) @FatimaNadeem1
@QuickV Oh it's ok, those didn't help, I know how to read from a text file but ordering them and then printing the top 5 is what I'm struggling with.
@FatimaNadeem1 split the string into an array and then sort it in reverse. I forgot the exact thing for sorting but just google how to sort python list in reverse
Ah sorry they didn't work [email protected]
@QuickV Lol it's fine
@IMayBeMe i'm guessing something like:
leaderboardlist.sort(reverse=True)
but i'll google it
k :P @FatimaNadeem1
@FatimaNadeem1 that will return descemding order. Ig thats what u need
yes but im assuming the sort command only works for string not integers. Do you know any ways I can sort numbers? @QuantumCodes
Traceback (most recent call last):
File "main.py", line 134, in
for line in file:#for every line in the list
ValueError: I/O operation on closed file.