【发布时间】:2016-05-19 00:19:06
【问题描述】:
我的目标是: 我目前正在尝试构建一个简单的故障排除程序。变量 current_question 旨在更新为嵌套 if 语句中每个输入/问题的值。这样代码就可以识别它当前正在询问的问题。因此,如果用户输入了错误的数据(不是是和不是),我可以循环回到当前问题
问题 但是,当我测试代码并继续到第二个问题时,此变量不会更新为问题 2、3、4 但每当我输入错误数据时,循环仅适用于第一个问题。我一直在尝试解决这个问题,但仍然没有找到解决方案,所以我认为这是一个很好的起点。
代码!
import time
current_question = ()
i = 1
while i <= 4:
q1 = str(input('Is your phone wet? ')).lower()
current_question = q1
i = i + 1
if q1 == 'yes' or q1 == 'Yes':
print('dry it out')
break
elif q1 == 'no' or q1 == 'No':
q2 = str(input('is your phone cracked? ')).lower()
current_question = q2
i = i + 1
if q2 == 'yes' or q2 == 'Yes':
print('replace screen')
break
elif q2 == 'no' or q2 == 'No':
q3 = str(input('are you able to download apps/videos? ')).lower()
current_question = q3
i = i + 1
if q3 == 'yes' or q3 == 'Yes':
print('delete any apps or videos that you don\'t need')
break
elif q3 == 'no' or q3 == 'No':
q4 = str(input('Is your phone unresponsive? ')).lower()
current_question = q4
i = i + 1
if q4 == 'yes' or q4 == 'Yes':
print('restart it')
break
elif q4 == 'no' or q4 == 'No':
print('contact the supplier')
break
while current_question != 'yes' and current_question != 'no':
print('try again')
time.sleep(1)
print(current_question)
【问题讨论】:
-
您将
current_question变量设置为用户给您的响应,这是您的意思吗? -
同样在你最后一个while循环中你永远不会改变
current_question的值,所以它是一个无限循环 -
您可能正在寻找一种方法来跟踪您在with something like this上的哪个问题
-
只是想知道为什么 current_question 默认为元组而不是字符串?
标签: python