I have a an issue in updating my dictionary from txt file. when ever a new player score is saved, the file creates a new dictionary. here is my code
def save_score(score):
filename = 'leaderboard.txt'
player = input('what is your name')
if os.path.exists(filename):
with open(filename, "r+") as leaderboard:
dictionary = json.load(leaderboard)
dictionary[player]=score
json.dump(dictionary,leaderboard)
print("score"+ " " + str(score)+ " "+ "for" + " " +str(player)+ " "+"saved.")
return
this is what I get when code is run.
{"Bob": 50,"Jane": 2,"Sarah": 5, "Amelia": 20}
then if a new player score get saved , it creates another separate dictionary.
{"Bob": 50,"Jane": 2,"Sarah": 5, "Amelia": 20} {"Bob": 50,"Jane": 2,"Sarah": 5, "Amelia": 20, "bobby": 20}
I would want the file to have just 1 dictionary and update the player name and score and also add new player in 1 dictionary instead of 2. thanks in advance.