Ask coding questions

← Back to all posts
Adding integers and strings into a text file
FatimaNadeem1 (0)

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
Comments
hotnewtop
FatimaNadeem1 (0)

Traceback (most recent call last):
File "main.py", line 134, in <module>
for line in file:#for every line in the list
ValueError: I/O operation on closed file.

IMayBeMe (548)

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

FatimaNadeem1 (0)

@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.

Coder100 (18917)

Treat db as a dict. So that means you can do something like:
db['num'] = 5
@FatimaNadeem1

FatimaNadeem1 (0)

@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")

IMayBeMe (548)

@FatimaNadeem1 you don’t the database is an alternative to a text file

FatimaNadeem1 (0)

@IMayBeMe Oh, so can you show me what the code will look like at the end for the leader board?

Coder100 (18917)

@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.

FatimaNadeem1 (0)

@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.

QuickV (115)

You shouldn't just let other people do all the work ;) @FatimaNadeem1

FatimaNadeem1 (0)

@QuickV i've done all of the code on my own, i just dunno how to add the leaderboard

FatimaNadeem1 (0)

@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

QuickV (115)

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

FatimaNadeem1 (0)

@QuickV I will but people are still not telling me how to do the leader board :(

IMayBeMe (548)

@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

FatimaNadeem1 (0)

@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.

QuickV (115)

Maybe nobody knows how .-. Maybe try googling it? @FatimaNadeem1

QuickV (115)

You might find it on google tho :P @FatimaNadeem1

FatimaNadeem1 (0)

@QuickV Any specific websites you know that may help?

QuickV (115)

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.

FatimaNadeem1 (0)

@QuickV There's no info about text files in there

QuickV (115)

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

FatimaNadeem1 (0)

@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.

IMayBeMe (548)

@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

FatimaNadeem1 (0)

@IMayBeMe i'm guessing something like:
leaderboardlist.sort(reverse=True)
but i'll google it

QuantumCodes (25)

@FatimaNadeem1 that will return descemding order. Ig thats what u need

FatimaNadeem1 (0)

yes but im assuming the sort command only works for string not integers. Do you know any ways I can sort numbers? @QuantumCodes