如何永久保存变量,以便当我单击运行时它不会重新启动为原始命令-python

时间:2019-11-03 07:54:46

标签: python

所以我有问题,我想计算python中的数字。我想要它,以便如果我问一个问题,用户得到10分,但是如果单击运行,它将变量重新回到0,因为变量等于零。有没有办法永久存储点,以便当我再次运行时,这些点仍然存在。

我是菜鸟,所以我不知道该怎么做 我也刚开始学习python,所以我不了解很多,所以您能以简单的术语向我解释一下,因为我刚开始学习却一无所知

1 个答案:

答案 0 :(得分:2)

我认为您应该看看如何在python中读取和写入文本文件

#get score in your program and then do this
score = 10
def saveScore(x):
    file = open("myfile.txt","a")
    file.write(str(x)+'\n')
    file.close()
def readScore():
    file = open("myfile.txt","r")
    return file.readlines()[-1]

#save score everytime time it changes
saveScore(score)
#this function will score of last saved state
score = readScore()