2015-05-05 147 views
2

所以我的while循環只是保持循環,即使它不應該,如果只有1個條件的循環工作,然後進行下一行代碼,但是當我添加OR語句它不會工作,我敢肯定這是非常愚蠢的事情,但我只是一個初學者,並試圖研究這一點。雖然循環與多個條件

Choice = input("What would you like to do? New Game, Continue, or Quit?").upper() 
while Choice != "NEW GAME" or Choice != "QUIT": 
    print ("That input was invalid, please try again.") 
    Choice = input("What would you like to do? New Game, Continue, or Quit? ").upper() 
    if Choice == "QUIT": 
     quit() 

回答

5

條件

Choice != "NEW GAME" or Choice != "QUIT" 

永遠是True。任何值Choice將不是"NEW GAME"或不是"QUIT"。相反,使用:

Choice != "NEW GAME" and Choice != "QUIT": 
+0

這很簡單....我一直認爲和意味着兩個條件需要是真實的,但工作,謝謝:) –

+0

這正是'和'的意思。但是這種情況下的條件是'!=',而不是'='。 –

0

我猜你想要一個and那裏,沒有一個or ...

因爲Choice不能在同一時間"NEW GAME""QUIT"不同。換句話說,無論Choice的值是什麼,您的環路條件始終爲True

2

我認爲你正在尋找

while Choice not in ["New Game", "Continue", "Quit"]

或更好地允許替代資本:

while Choice.upper() not in ["NEW GAME", "CONTINUE", "QUIT"]

也請uncapitalize變量Choice。當其他Python程序員看到一個以大寫字母開頭的變量時,他們首先假定它是一個類名。