我正在尝试将某些内容保存到.py文件中,当我保存它时,它会使该文件夹出现,但不转储其中的任何内容,我也不知道为什么!因此,我收到EOFError的错误:尝试加载数据时出现输入不足的情况。
def save():
ult = [player.name, player.account_balance, player.cash, player.hunger, player.job, player.skill, player.inventory, player.pin, player.fulltime_days_worked]
Save = input("Do you want to save? (y/n): ").lower().strip()
print("="*15)
if Save == "y":
with open("Savegame.py", "wb") as f:
pickle.dump(str(ult), f)
quit = input("Your session has been saved! Do you want to quit(y/n)").lower()
print("="*15)
if quit == "y":
shutdown.terminate()
elif quit == "n":
main_menu()
print("="*15)
def load():
save1 = input("Do you want to load last session?(y/n): ").lower()
print("="*15)
if save1 == "y":
with open("Savegame.py", "rb") as f:
ult = pickle.load(f)
main_menu()
elif save1 == "n":
main_menu()
我希望数据也存储在我也尝试过.txt的.py文件中
更新
这对我有用!由于某种原因,除非我加载它并给出EOFError,否则它不会保存数据,然后它会保存数据。所以这是我的解决方法。我提供了在代码的开头加载数据的选项,它的工作原理就像一个魅力!
def Save_game():
try:
ult = [player.name, player.account_balance, player.cash, player.hunger, player.job, player.skill, player.inventory, player.pin, player.fulltime_days_worked]
save = input("do you want to save?(y/n) ").lower()
if save == "y":
with open('Savegame.py', 'wb') as file:
pickle.dump(ult, file)
with open('Savegame.py', 'rb') as file:
ult = pickle.load(file)
except EOFError:
print("You saved the game!")
quit = input("Do you want to quit? (y/n): ")
if quit == "y":
shutdown.terminate()
elif quit == "n":
main_menu()