Python “如何写入此脚本以验证结束”;如果他们想退出此计划”;
需要在最后说“你想退出吗”并选择“是”或“否”,然后在输入后说“你确定吗”,如果有其他选择,则说“是”或“否”无效输入并再次提问。写得更像pythonic: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 ('
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