Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何写入此脚本以验证结束”;如果他们想退出此计划”;_Python_Validation - Fatal编程技术网

Python “如何写入此脚本以验证结束”;如果他们想退出此计划”;

Python “如何写入此脚本以验证结束”;如果他们想退出此计划”;,python,validation,Python,Validation,需要在最后说“你想退出吗”并选择“是”或“否”,然后在输入后说“你确定吗”,如果有其他选择,则说“是”或“否”无效输入并再次提问。写得更像pythonic: problem = False while problem == False: foo = open("solutions.txt","r") print("What is wrong with your device?") issue=input() if (('wet' in issue) and ('

需要在最后说“你想退出吗”并选择“是”或“否”,然后在输入后说“你确定吗”,如果有其他选择,则说“是”或“否”无效输入并再次提问。

写得更像pythonic:

problem = False
while problem == False:
    foo = open("solutions.txt","r")
    print("What is wrong with your device?")
    issue=input()

    if (('wet' in issue) and ('water' in issue)):
        solutions = foo.readlines()
        print(solutions[0]+solutions[1])
        problem = True

       # (and so on). 

首先,请注意,您可以简单地使用带有True的永久while循环,并在需要时中断循环,或者作为一个问题并中断循环,答案是否定的。其次,对于打开文件,您最好使用
语句,在块的末尾自动关闭文件。如果你的文件很大,你最好使用
readline
方法来读取前两行。

循环中的下一行是:

while True:
    with open("solutions.txt") as foo:
        issue = input("What is wrong with your device?")
        if 'wet' in issue and 'water' in issue:
            print(foo.readline() + foo.readline())
        question = input("Do you want to continue? (Y/N)")
        if question == 'N':
           break
 answer = raw_input("Would you like to exit?  Enter 'Yes' or 'No': ")
 while answer not in ["Yes","No"]:
     answer = raw_input("Would you like to exit?  Enter 'Yes' or 'No': ")
 if answer == "Yes"
     problem = True    # This will cause you to exit the loop