这是我到目前为止我的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循环后停止运行,我不知道为什么
答案 0 :(得分:0)
在datafile_1.read()
和datafile_1.read()
with open("keywords_1","r+") as datafile_1
- 您输入的文件名没有文件类型,因此该函数将停止,因为没有这样的文件或目录可供阅读。
尽量不要使用global
个变量,只是不安全。