2014-05-16 59 views
2

我已經測試了下列程序,並且沒有錯誤。但是,無論何時輸入"hangman",它都不會啓動名爲"if response_2"if語句代碼塊。爲什麼它沒有運行它?Python 2.7:如果代碼塊不工作

response_2 = raw_input("What would you like to play? Hangman or Word Guess?") 
    if response_2 == ("Hangman", "hangman"): 
     print "Running Hangman..." 
     print "Catching Criminals..." 
     print "Building Gallows..." 
     print "Getting that one song boy from Pirate's of the Carribean" 
    elif response_2 == ("Word_Guess", "word_guess", "word guess", "Word Guess", "Word guess", "word Guess", "Word_guess", "word_Guess"): 
     print "Not completed yet" 
+1

請將代碼粘貼到問題中。隨着時間的推移,鏈接變得糟糕,那麼這會離開SO? –

+0

你的情況可以**不會**爲「真」。爲什麼這個街區會跑? – jonrsharpe

+1

它需要是一個「in」,而不是與元組的平等。輸入的內容不等於它......它是它的一員。 –

回答

8

這是因爲你直接比較的元組==,這總是會給False作爲raw_input給出了一個字符串,而不是tuple。您需要檢查是否有任何一個響應在序列中。與elif內的類似的比較同樣

if response in ('Hangman', 'hangman'): 

:與in做到這一點。

+0

非常感謝! – The10thDoctor

+0

@ The10thDoctor太棒了,很高興我能幫到你。如果我的回答對你有幫助,你是否介意在寬限期過後接受它?您可以通過點擊投票計數下面的勾號來做到這一點,以便它變成綠色並保持綠色。謝謝。 –