我不确定为什么但是当我执行这部分代码时没有任何反应。
while (True) :
choice = str(input("Do you want to draw a spirograph? (Y/N) "))
if choice == 'n' or 'N' :
break
elif choice == 'y' or 'Y' :
<CODE>
else :
print("Please enter a valid command.")
choice = input("Do you want to draw a spirograph? (Y/N)")
答案 0 :(得分:10)
它无法正常工作,因为'N'
字面值始终在True
语句中评估为if
。
您的if
条件目前为if choice == 'n' or 'N' :
,相当于if (choice == 'n') or ('N')
,无论变量True
的值如何,它都将始终评估为choice
,因为文字'N'
始终评估为True
。
相反,请使用以下
之一if choice == 'n' or choice == 'N' :
if choice in 'nN' :
if choice in ('n', 'N') :
同样适用于您的elif
区块。您可以阅读有关Truth Value testing here的更多信息。
答案 1 :(得分:4)
这个表达不能做你想做的事:
choice == 'n' or 'N'
它被解释为:
(choice == 'n') or 'N'
你可能想尝试这样做:
choice in 'nN'