Reputation: 157
I am creating a .txt file to store scores in my snake game. The problem is writing back into the file. my current idea goes somewhat like this:
for line in open("scoreboard.txt", "r+"):
line = scorelist[y] + namelist[y]
I've already read through the scores, made them into a list, and incorporated a new score, but I can't figure out how to simultaneously cycle through both a line and the lists i'm storing the data in to process, and overwrite the old scores.
Upvotes: 0
Views: 75
Reputation: 7360
You could use JSON encoding to store simple objects into a file:
import json
myscore = [1,2,5]
mynames = ["foo","bar","baz"]
#save
with open("scores.json","w") as f:
json.dump({'score' : myscore, 'names': mynames},f)
#load
with open("scores.json","r") as f:
content = json.load(f)
loadedScore = content['score']
loadedNames = content['names']
Upvotes: 1