为什么我的代码在使用for循环后停止

时间:2016-03-30 00:08:29

标签: python

这是我到目前为止我的gcse计算控制评估的代码。我试图让用户回答一个问题,然后python将挑选出特定的单词(与文件中存储的单词相同),然后将其链接到解决方案。

global line
global userinput
global word
def main():
    global userinput
    name=input("What is your name")
        print("Hello " +name+ " and welcome to our troubleshooting system!")
    userinput=input("What is the problem with your mobile device?")
    userinput=userinput.split()
    if userinput=="":
        print("Please try again")
    power_problems()

def power_problems():
    global word
    global line
    global userinput
    with open("keywords_1","r+") as datafile_1:
    datafile_1.read()
        for line in datafile_1:
            if "userinput" in line:
                print("Hold the restart button for 30 seconds")
            else:
                phone_problems()

def phone_problems():
    global word
    global line
    global userinput
    with open("keywords_2", "r+") as datafile_2:
    datafile_2.read()
        for line in datafile_2:
            if "userinput" in line:
                print("Take the phone to the manufacturer to get a replacement")
if __name__ == '__main__':
    main() 

我的问题是代码在函数“power_problems”中使用for循环后停止运行,我不知道为什么

1 个答案:

答案 0 :(得分:0)

  1. datafile_1.read()datafile_1.read()

  2. 检查缩进
  3. with open("keywords_1","r+") as datafile_1 - 您输入的文件名没有文件类型,因此该函数将停止,因为没有这样的文件或目录可供阅读。

  4. 尽量不要使用global个变量,只是不安全