每次在Python中运行程序时如何更改i变量中的数字?

时间:2017-02-18 13:13:34

标签: python variables time

print ('What type of device?')
device=input()
if device == 'phone':
    print('What make of phone is it? iphone or android?')
    make=input()
    if make == 'iphone':
        print ('Which model is it? 5, 6 or 7?')
        model = input()
        print ('Which version?')
        version = input()
        print('How much memory, 8GB, 16GB or 32GB?')
        memory = input()
        print ('What is the problem?')
        problem = input()
        if model == '5' and version == 'S' and memory == '16GB':
            textfile=open('iphone5S16GB.txt','r')
            iphone5S16GB=textfile.readlines()
            if 'apps' in problem or 'close down' in problem or 'slow' in problem:
                print (iphone5S16GB[0])
            elif 'screen' in problem or 'display' in problem or 'monitor' in problem:
                print (iphone5S16GB[1])
            elif 'music' in problem or 'download' in problem:
                print(iphone5S16GB[2])
            else:
                i=0
                with open('casenumbers.txt','a+') as f:
                  f.write(str(i+1) + ' = casenumber \n')
                  f.close()
        elif model == '6' and version == 'S' and memory == '32GB':
            textfile=open('iphone6S32GB.txt','r')
            iphone6S32GB=textfile.readlines()
            if 'home button' in problem or 'touch id' in problem:
                print (iphone6S32GB[0])
            elif 'battery' in problem or 'charging' in problem:
                print(iphone6S32GB[1])
            elif 'hot' in problem or 'overheating' in problem:
                print(iphone6S32GB[2])
            else:
              with open('casenumbers.txt','a+') as f:
                f.write('hi this is the second test' '\r\n')
                f.close()
    else:
        print('The program only has examples for two types of iphone'
    ' but if this program were to be expanded then more solutions could be found'
    ' for many different devices')
else:
    print('The program only has examples for two types of iphone'
' but if this program were to be expanded then more solutions could be found'
' for many different devices')

此程序旨在询问用户使用的设备类型,然后询问有关设备的一些问题。然后它询问用户有关其设备的问题。然后程序将返回与用户给出的问题相对应的解决方案。如果没有找到解决方案,则应为其分配案例编号并存储在另一个文本文件中。我的程序工作,我只需要知道每次运行程序时如何更改'i'变量。这是在else语句中。我的程序只需要有示例,一旦我知道如何做到这一点,那么我可以在下一个else语句中实现它。任何帮助都会很棒。谢谢。

2 个答案:

答案 0 :(得分:0)

也许你可以将'i'存储在一个文本文件中。每次运行程序时,都可以从文件中读取,增加“i”,使用它然后将其替换为同一个文件

答案 1 :(得分:0)

要在每次运行程序时增加“i”,您需要将“i”保存在文件中。例如(我选择的文件名不是特别的):

i=0
try:
  with open('lastcasenumber.txt','r') as f:
    i = 1 + int(f.read())
except:
  pass
with open('casenumbers.txt','a+') as f:
  f.write(str(i+1) + ' = casenumber \n')
with open('lastcasenumber.txt','w') as f:
  f.write(str(i))