2013-12-16 38 views
0

我剛剛不到一週前介紹給python,我試圖做一個簡單的二十一點遊戲。我已經寫出了程序的核心,它似乎忽略了原始輸入,並且不斷循環遍歷for條件語句。無論我打字,打字,留下什麼都無所謂。這裏的代碼:輸入被忽略,函數循環在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() 

我很茫然,並希望得到任何幫助。

謝謝!

+0

當遊戲繼續時,您應該考慮使用'while'循環而不是自己調用'main_game()'。 –

回答

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" 

(同所有其它elifs)

+0

太棒了,非常感謝! – user3105806

0

只是作爲一個額外注,你真的不應該打電話main_game()內部main_game()。相反,你應該用一個while循環包裝所有的代碼。像這樣

def main_game(): 
while True: 
    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 in ("hit", "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) 
    elif input in ("Stay", "stay") and total == 21: 
     print "Holy Cow! A perfect hand!" 
    elif input in ("Stay", "stay") and total < dealer: 
     print "Sorry, the dealer had %s" % (dealer) 
    elif input in ("Stay", "stay") and total > 21: 
     print "Sorry, you have more than 21" 
    else: 
     print "Could you say again?"