2014-05-04 24 views
0

我如何得到它,这样我可以在例如循环在Python 3

start = input(("Would you like to start? ")) 
    while start == "yes" or "YES" or "Yes": 

然后while循环的工作“或”以后我的代码

start = input(("Would you like to start again? ")) 
    if start == "no" or "No" or "NO": 
     break 

当我试试这个代码它不起作用。无论我输入什么,它都会在开始时启动代码并在结束时中断。谁能帮忙?

回答

2

由于or==更高的优先级,

start == "yes" or "YES" or "Yes": 

将被评估为

(start == "yes") or ("YES") or ("Yes") 

你可以简单地做

while start.lower() == "yes": 

用同样的方法,

if start.lower() == "no": 
+0

start.lower()in('y','yes') – sshashank124

0

or之间的每条语句是分开的。所以你实际上是否 start == "yes"True"YES"True
因为"YES"不是一个空字符串,则视为True布尔值。

我想改变它的东西,如:

while (start == "yes") or (start == "YES") or (start == "Yes"): 

甚至:

while start.lower() == "yes": 
0

取而代之的是:

while start == "yes" or "YES" or "Yes": 

做到这一点(同样与如果作为):

while start == "yes" or start== "YES" or start == "Yes": 

,或者甚至更好,这样做:

while start.lower() == "yes": 

你也可以这样做:

while start.lower().startswith('y'): 

因此,如果用户输入任何以“Y”,它会做无论是在同时,声明。