Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to save player stats in python

To save player stats in python, first import json . Then find the dictionary or variable you want to save eg.player_dict = {'name':'TommyDoesASCII','hp':100,'attack_damage':15} And for the actual save and load, you want to type json.dump( player_dict, open( player_dict['name']+".json", 'w' ) ) this code saves the player dictionary. And to load,global player_dict loadName = input('What is the name of your file?\n') player_dict = json.load(open( loadName+".json" ) ) This will pretty much look up the file name and open it. Hope this helped!


Don't do:

json.dump( player_dict, open( player_dict['name']+".json", 'w' ) ).

You aren't closing the file! If you write to a file but don't close it, it's not always saved, due to buffering optimizations. You need to do file.close() when you are done with it.


@liltaco I actually tried that and it broke the program