简体   繁体   English

Python 字典未保存到数据 - Pickle

[英]Python dictionary is not saving to data - Pickle

So I am making a game with a leaderboard, so, but it still resets every time.所以我正在制作一个带有排行榜的游戏,所以,但它仍然每次都会重置。 This is my code:这是我的代码:

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

When I use a dictionary to test test_dictionary['thing one'] = 'thing two' it works fine to add it.当我使用字典测试test_dictionary['thing one'] = 'thing two'时,添加它可以正常工作。 But every time I put it into the code above, it will just reset.但是每次我把它放到上面的代码中,它都会重置。 Does anyone know why?有谁知道为什么?

Try putting in different answers for each one.尝试为每个问题输入不同的答案。 If you are testing, you should put in different things, instead of the same one.如果您正在测试,您应该放入不同的东西,而不是相同的东西。 It doesn't work for me if I put in the same answers.如果我输入相同的答案,它对我不起作用。

try saving your score by writing in to txt file rather than using a pickle as it is a much more advanced way.尝试通过写入 txt 文件而不是使用 pickle 来保存您的分数,因为这是一种更高级的方法。 With something like this:像这样:

with open("score.txt", "w") as file:
    file.write(score)

And at the beginning of you game just load last saved score with something like this:在游戏开始时,只需加载最后保存的分数,如下所示:

try:
    with open("score.txt", "r") as file:
        score = int(file.readline())
except Exception:
    score = 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM