我正在努力在一个腌制文件中附加一个列表.这是代码:
#saving high scores to a pickled file
import pickle
first_name = input("Please enter your name:")
score = input("Please enter your score:")
scores = []
high_scores = first_name, score
scores.append(high_scores)
file = open("high_scores.dat", "ab")
pickle.dump(scores, file)
file.close()
file = open("high_scores.dat", "rb")
scores = pickle.load(file)
print(scores)
file.close()
Run Code Online (Sandbox Code Playgroud)
我第一次运行代码时,会打印出名称和分数.
第二次运行代码时,它会输出2个名称和2个分数.
第三次运行代码时,它会输出第一个名称和分数,但它会覆盖第二个名称并使用我输入的第三个名称和分数进行分数.我只是想让它继续添加名称和分数.我不明白为什么它保存名字并覆盖第二个名字.