阅读更多:apachecn/python-code-anal
导入
import random
cards
cards = {
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "Jack",
11: "Queen",
12: "King",
13: "Ace",
}
get_user_bet()
def get_user_bet(cash):
while True:
try:
bet = int(input("What is your bet? "))
if bet < 0:
print("Bet must be more than zero")
elif bet == 0:
print("CHICKEN!!\n")
elif bet > cash:
print("Sorry, my friend but you bet too much")
print(f"You only have {cash} dollars to bet")
else:
return bet
except ValueError:
print("Please enter a positive number")
draw_3cards()
def draw_3cards():
round_cards = list(cards.keys())
random.shuffle(round_cards)
card_a, card_b, card_c = round_cards.pop(), round_cards.pop(), round_cards.pop()
if card_a > card_b:
card_a, card_b = card_b, card_a
return (card_a, card_b, card_c)
play_game()
def play_game():
"""Play the game"""
cash = 100
while cash > 0:
print(f"You now have {cash} dollars\n")
print("Here are you next two cards")
card_a, card_b, card_c = draw_3cards()
print(f" {cards[card_a]}")
print(f" {cards[card_b]}\n")
bet = get_user_bet(cash)
cash -= bet
print(f" {cards[card_c]}")
if card_a < card_c < card_b:
print("You win!!!")
cash += bet * 2
else:
print("Sorry, you lose")
print("Sorry, friend, but you blew your wad")
main()
def main():
print("""
Acey-Ducey is played in the following manner
The dealer (computer) deals two cards face up
You have an option to be or not bet depending
on whether or not you feel the card will have
a value between the first two.
If you do not want to bet, input a 0
""")
while True:
play_game()
keep_playing = input("Try again? (yes or no) ").lower() in ["yes", "y"]
if not keep_playing: break
print("Ok hope you had fun")
if __name__ == "__main__": main()