我想设计一个受流程图启发的'调查',但我不明白为什么它不起作用。我想要没有包装的简单物品,因为我还没有把它们包裹起来。
它应该如何工作:
Q1-输入为是-> Q2-输入为-Q3->输入是-...-Q9-输入为-> Q10
Q1-输入为否->中断
Q1-可能输入->重新Q1
[当我当前输入与是或否不同的东西时,它从开头(Q1)开始。.我可以让它在所有问题中重复IF语句,直到输入为是或否吗?
while True:
x = input ('question_text' )
if x.lower () == 'yes':
x = input ( 'question2_text' )
if x.lower () == 'yes':
x = input ( 'question3_text' )
if x.lower () == 'yes':
ETC...
if x.lower () == 'no':
print ( 'No.' )
break
else:
print ('ONLY YES/NO')
if x.lower () == 'no':
print ( 'No.' )
break
else:
print ('ONLY YES/NO')
if x.lower () == 'no':
print ( 'No.' )
break
else:
print ('ONLY YES/NO')
如果不对当前结构上的所有案例重复相同的else语句,您将无法做到这一点。
但是,您可以对输入使用一个函数,该函数将反复对其求值。
例如:
def yes_or_no_question(question):
answer = input(question).upper()
if answer in ["YES", "NO"]:
print(answer)
return answer == "YES"
print("Only YES or NO")
return yes_or_no_question(question)
while True:
if not yes_or_no_question('question_text'):
break
if not yes_or_no_question('question2_text'):
break
print("Yes to all, starting again!")