Skip to content
← Back to Community
Adding integers and strings into a text file
Profile icon
FatimaNadeem1

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
Voters
Profile icon
FatimaNadeem1
Comments
hotnewtop
Profile icon
FatimaNadeem1

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.

Profile icon
IMayBeMe

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

Profile icon
FatimaNadeem1

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

Profile icon
Coder100

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

Profile icon
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")

Profile icon
IMayBeMe

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

Profile icon
FatimaNadeem1

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

Profile icon
Coder100

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

Profile icon
FatimaNadeem1

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

Profile icon
Coder100

@FatimaNadeem1 what isn't working

Profile icon
QuickV

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

Profile icon
FatimaNadeem1

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

Profile icon
FatimaNadeem1

@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

Profile icon
QuickV

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

Profile icon
FatimaNadeem1

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

Profile icon
IMayBeMe

@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

Profile icon
FatimaNadeem1

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

Profile icon
QuickV

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

Profile icon
FatimaNadeem1
Profile icon
QuickV

You might find it on google tho :P @FatimaNadeem1

Profile icon
FatimaNadeem1

@QuickV Any specific websites you know that may help?

Profile icon
QuickV

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.

Profile icon
FatimaNadeem1

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

Profile icon
QuickV

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

Profile icon
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.

Profile icon
IMayBeMe

@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

Profile icon
QuickV

Ah sorry they didn't work [email protected]

Profile icon
FatimaNadeem1

@QuickV Lol it's fine

Profile icon
FatimaNadeem1

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

Profile icon
QuickV
Profile icon
QuantumCodes

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

Profile icon
FatimaNadeem1

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