2014-05-04 70 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」,它會做無論是在同時,聲明。