Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字典未保存到数据-Pickle_Python_Dictionary_Pickle - Fatal编程技术网

Python字典未保存到数据-Pickle

Python字典未保存到数据-Pickle,python,dictionary,pickle,Python,Dictionary,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\u dictionary['thingone']='

所以我制作了一个排行榜游戏,但是每次都会重置。这是我的代码:

config_dictionary[url] = short
with open('config.dictionary', 'wb') as config_dictionary_file:
  pickle.dump(config_dictionary, config_dictionary_file)
  print(config_dictionary)

当我使用字典测试
test\u dictionary['thingone']='thingtwo'
时,添加它就可以了。但是每次我把它放到上面的代码中,它就会重置。有人知道为什么吗?

尝试通过写入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 
在游戏开始时,只需加载上次保存的分数,如下所示:

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 

试着为每个问题输入不同的答案。如果你在测试,你应该放不同的东西,而不是同一个。如果我输入相同的答案,这对我不起作用。

如果您有任何问题或需要进一步帮助,请告诉我。