我剛剛不到一週前介紹給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()
我很茫然,並希望得到任何幫助。
謝謝!
當遊戲繼續時,您應該考慮使用'while'循環而不是自己調用'main_game()'。 –