everyone.
I was tinkering with ways to store dictionaries and retrieve them later, loading them into and replacing the existing dictionary values.
I was intending to use this as a way to store game data in a simple text-based game.
# Player dictionaries
p1 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
p2 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
p3 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
p4 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
p5 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
p6 = {'name' : "placeholder", 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0,
'atk': 0, 'def': 0, 'cyra' : 0, 'monero' : 0, 'bits': 0, 'dollars': 0,
'nordas': 0,}
#save function
import pickle
def save() :
f = open("p1.txt","w")
f.write( str(p1) )
f.close()
f = open("p2.txt","w")
f.write( str(p2) )
f.close()
f = open("p3.txt","w")
f.write( str(p3) )
f.close()
f = open("p4.txt","w")
f.write( str(p4) )
f.close()
f = open("p5.txt","w")
f.write( str(p5) )
f.close()
f = open("p6.txt","w")
f.write( str(p6) )
f.close()
print ("The game was saved.")
To my delight, I discovered that this does work, and it creates six files names "p1.txt", "p2.txt", and so forth in the folder that the program is saved. It looks exactly like the original dictionary:
{'name': 'placeholder', 'hp': 0, 'str': 0, 'stm': 0, 'mana': 0, 'atk': 0, 'def': 0, 'cyra': 0, 'monero': 0, 'bits': 0, 'dollars': 0, 'nordas': 0}
However, upon attempting to try and create an import_save()
function, I ran into a problem. I couldn't use unpickle()
, because that's not how I exported the dictionary in the first place. Any tips on how this could be solved?
I was also looking to find a way that the imported dictionary could over-write the existing dictionary? I couldn't find anything about it... I'm not against re-writing the code if that is needed.