首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次在Python中运行程序时,如何更改i变量中的数字?

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

Stack Overflow用户
提问于 2017-02-18 21:13:34
回答 2查看 40关注 0票数 1
代码语言:javascript
复制
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语句中实现它。任何帮助都是最好的。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-18 21:34:23

要在每次程序运行时增加'i‘,您需要将'i’保存在一个文件中。例如(我选择的文件名不是特殊的):

代码语言:javascript
复制
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))
票数 0
EN

Stack Overflow用户

发布于 2017-02-18 21:20:44

也许你可以把'i‘存储在一个文本文件中。每次运行该程序时,您都可以从文件中读取,递增'i',使用它,然后在同一文件中替换它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42315605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档