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

时间:2014-05-16 17:48:56

标签: python python-2.7 if-statement

我已经测试了以下程序,并且没有错误。但每当我输入"hangman"时,它都不会启动名为if的新"if response_2"语句代码块。为什么不运行它?

    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 个答案:

答案 0 :(得分:8)

这是因为您直接将==的元组进行比较,而False总是会给raw_input,因为tuple会给出一个字符串,而不是in {1}}。您需要检查序列中是否有任何一个响应。使用if response in ('Hangman', 'hangman'): 执行此操作:

elif

与{{1}}中的类似比较相同。