输入被忽略,函数在for语句处循环

时间:2013-12-16 02:22:24

标签: python

我不到一个星期前就被介绍给python,我正在尝试制作一个简单的二十一点游戏。我已经写出了程序的核心,它似乎忽略了原始输入,并且只是不断地循环遍历for条件。无论我打字,打击,留下什么,都无关紧要。这是代码:

def main_game():
    number = random.randint(1,13)
    card_type_number = random.randint(1,4)
    total = 0
    dealer = random.randint(1,21)
    input = raw_input("Would you like to Hit or Stay? \n")

    if input == "hit" or "Hit":
        card_number = numberConverter(number)
        card_type = typeConverter(card_type_number)
        new_amount = number
        print "You got a %s of %s. You currently have %s. \n" % (card_number, card_type, number)
        total += number
        number = random.randint(1,13)
        card_type_number = random.randint(1,5)
        main_game()
    elif input == ("Stay" or "stay") and total == 21:
        print "Holy Cow! A perfect hand!"
        main_game()
    elif input == ("Stay" or "stay") and total < dealer:
        print "Sorry, the dealer had %s" % (dealer)
        maingame()
    elif input == ("Stay" or "stay") and total > 21:
        print "Sorry, you have more than 21"
        main_game()
    else:
        print "Could you say again?"
        main_game()

我很茫然,不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:4)

if input == "hit" or "Hit":

这意味着if (input == "hit") or ("Hit"),这总是正确的。

尝试

if input == "hit" or input == "Hit":

或者

if input in ("hit", "Hit"):

或者,甚至更好:

if input.lower() == "hit"

(所有其他精灵都一样)