定义变量后如何更改其数值?

时间:2018-09-16 17:11:51

标签: python python-3.x python-2.7

在定义变量后如何更改其值,然后在下次运行程序时将变量更改为新值。

p = 0
result = int(input("Test "))
if result == 3:
   points = p+3
   p = points

基本上,我需要它,以便下次运行此代码时,“ p”已设置为3,然后此后的时间更改为6,依此类推。

1 个答案:

答案 0 :(得分:0)

import os

with open('tmp.txt', 'r') as file:
     p = file.read()
os.remove("tmp.txt")
result = int(input("Test "))
if result == 3:
   points = int(p)+3
   p = points
   with open('tmp.txt', 'w') as file:
       file.write(str(p))

尝试一下。在运行脚本之前,在与代码相同的文件夹中创建文件“ tmp.txt”。