Python 词典未保存到数据 - Pickle
Python dictionary is not saving to data - Pickle
所以我正在制作一个带有排行榜的游戏,但是每次它仍然会重置。这是我的代码:
config_dictionary[url] = short
with open('config.dictionary', 'wb') as config_dictionary_file:
pickle.dump(config_dictionary, config_dictionary_file)
print(config_dictionary)
当我使用字典测试时 test_dictionary['thing one'] = 'thing two'
添加它工作正常。但是每次我把它放到上面的代码中,它都会重置。有谁知道为什么?
尝试通过写入 txt 文件而不是使用 pickle 来保存你的分数,因为这是一种更高级的方法。像这样:
with open("score.txt", "w") as file:
file.write(score)
在游戏开始时,只需加载上次保存的分数,如下所示:
try:
with open("score.txt", "r") as file:
score = int(file.readline())
except Exception:
score = 0
尝试为每个问题输入不同的答案。如果你正在测试,你应该放入不同的东西,而不是相同的东西。如果我输入相同的答案,它对我不起作用。
所以我正在制作一个带有排行榜的游戏,但是每次它仍然会重置。这是我的代码:
config_dictionary[url] = short
with open('config.dictionary', 'wb') as config_dictionary_file:
pickle.dump(config_dictionary, config_dictionary_file)
print(config_dictionary)
当我使用字典测试时 test_dictionary['thing one'] = 'thing two'
添加它工作正常。但是每次我把它放到上面的代码中,它都会重置。有谁知道为什么?
尝试通过写入 txt 文件而不是使用 pickle 来保存你的分数,因为这是一种更高级的方法。像这样:
with open("score.txt", "w") as file:
file.write(score)
在游戏开始时,只需加载上次保存的分数,如下所示:
try:
with open("score.txt", "r") as file:
score = int(file.readline())
except Exception:
score = 0
尝试为每个问题输入不同的答案。如果你正在测试,你应该放入不同的东西,而不是相同的东西。如果我输入相同的答案,它对我不起作用。