简体   繁体   English

Python 2.7:如果代码块不起作用

[英]Python 2.7: if code block not working

I have tested out the following program, and there are no errors. 我测试了以下程序,没有错误。 But whenever I enter "hangman" it won't start the new block of if statement code named "if response_2" . 但每当我输入"hangman"它都不会启动名为"if response_2"的新if语句代码块。 Why is it not running it? 为什么不运行它?

    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"

This is because you are directly comparing to the tuple with == , which will always give False as the raw_input gives a string, not a tuple . 这是因为你直接用带有==的元组进行比较 ,它总是给出False因为raw_input给出了一个字符串,而不是一个tuple You need to check if any one of the responses is in the sequence. 您需要检查序列中是否有任何一个响应。 Do this with in : 有这样做in

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

Likewise with the similar comparison within the elif . elif的类似比较相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM