2014-03-02 38 views
0

我正在製作一個簡單的猜謎遊戲,並遇到了一些問題。首先,我是Python新手,除了一些批處理外,這是我的第一個編程語言。 當我嘗試執行以下代碼時,即使條件爲真,elifelse也不會與之交互。它似乎總是迴應if,因爲它總是如此。多條件雖然循環

#Calder Hutchins 3-1-14 
#Guess Game 
import time 
playGame = "" 
guessTry = 3 
print "Would you like to play a game? (y/n)" 
while playGame != "y": 
    playGame = raw_input(">") 
    if playGame == "y" or "n": 
     print "Great! Let's get started!" 
     print "Answer the following questions correctly to level up!" 
    elif playGame == "n": 
     print "Very well...." 
     time.sleep(2) 
     quit() 
    else: 
     print "Invalid answer, please re-type." 
print "end of the while, test." 

此外,我想知道流量控制,因爲它對我來說是新的。有沒有辦法讓它更有效率,我粘貼的代碼。

編輯:這不是重複的......如果你們有些人會花時間看看我的問題,你會發現它不是。在Stack Overflow上看到這麼多次發生,我感到非常失望。

+0

除了這個我想你是不是很瞭解使用if/elseif的。我建議看看http://docs.python.org/2/tutorial/controlflow.html – Hugoagogo

+0

好的,我會看看它,我正在使這個腳本更多地學習這個概念。 –

+1

在這個例子中,我認爲如果if語句的計算結果爲false,那麼if語句簡單地說就是playGame ==「y」,如果計算結果爲false,它將移動到其他情況,所以你非常接近。 – Hugoagogo

回答

0

電腦很笨,你需要明確說明你想要評估什麼。

if playGame == "y" or playgame == "n": 

在你的版本將始終爲true,因爲當兩個字符串進行比較,將得到評估,以Truestrstr永遠是True

>>> bool('a' or 'c') 
True