<p>我刚刚完成了与上面类似的解决方案。在玩了2轮或更多轮之后,递归调用会很明显</p>
<pre><code>import random
def welcome():
print("Welcome to Python Acey Ducey Card Game\n")
print("Acey-ducey is played in the following manner: ")
print("\tthe dealer (computer) deals two cards faced up")
print("\tyou have an option to bet or not bet depending on")
print("\twhether or not you feel the card will have a value")
print("\tbetween the first two.")
print("\tIf you do not want to bet, enter a $0 bet.\n")
def get_bet(balance):
while True:
bet = input("Enter your bet: ")
try:
bet = int(bet)
if bet <= balance and bet >= 0:
return bet
elif bet < 0:
print("You can't bet negative amounts!")
else:
print("You don't have enough cash to bet that!")
except:
print("You must enter a number bet!")
def game_round(bankbalance):
print("\nThese cards are open on the table:")
firstcard = random.randint(1,13)
print("\nFirst card:", firstcard)
secondcard = random.randint(1,13)
print("Second card:", secondcard)
playerinput = get_bet(bankbalance)
dealercard = random.randint(1,13)
dealercard = int(dealercard)
print("The card you drew was", (dealercard), "!")
if dealercard > firstcard and dealercard < secondcard or dealercard < firstcard and dealercard > secondcard:
print("You win!")
return playerinput
else:
print("You lost")
return -playerinput
def main():
welcome()
bankbalance = 100
print("You currently have $", (bankbalance))
while True:
playagain = input("Enter [Y] to play a game, any other key to exit: ")
if playagain.lower() == ("y"):
bankbalance = game_round(bankbalance)
print("\nYou currently have $", (bankbalance))
else:
break
print("Thanks for playing- Goodbye!")
main()
</code></pre>