import random card_values = {'2':2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11} deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] * 4 random.shuffle(deck) def deal_card(deck): return deck.pop() def calculate_total(hand): total = sum(card_values[card] for card in hand) if 'A' in dealer_hand and total > 21: total -= 10 return total #player turn def player_turn(deck,player_hand): while True: player_choice = input ("Hit or Stand? ").lower() if player_choice == 'hit': player_hand.append(deal_card(deck)) total = calculate_total(player_hand) print("your hand:", player_hand,) if 'A' in player_hand and total <= 21: choice = input("You have an Ace. Do you want it to be 1 or 11? ") while choice not in ['1', '11']: choice = input("Invalid input. Please choose 1 or 11: ") if choice == '1': total = total + 1 if choice == '11': total = total + 11 print("total:", total) if total > 21: print("Bust! You lost.") return False if total == 21: print("You win") return False elif player_choice == 'stand': return True #dealer turn def dealer_turn(deck,dealer_hand): while calculate_total(dealer_hand) < 17 : dealer_hand.append(deal_card(deck)) return dealer_hand #running the game while True: player_hand = [deal_card(deck), deal_card(deck)] dealer_hand = [deal_card(deck), deal_card(deck)] print("your hand:", player_hand, "total:", calculate_total(player_hand)) print("Dealer's hand:", dealer_hand[0]) if player_turn(deck,player_hand): dealer_hand = dealer_turn(deck, dealer_hand) print("dealer's hand:", dealer_hand, "Total:", calculate_total(dealer_hand)) player_total = calculate_total(player_hand) dealer_total = calculate_total(dealer_hand) if dealer_total > 21 or player_total == dealer_total or player_total > dealer_total: print("you win") elif dealer_total > player_total: print("dealer wins!") else: print("it's a tie!") play_again = input("play again? (yes/no): ").lower() if play_again != 'yes': break if len(deck) < 10: deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] * 4 random.shuffle(deck) print("thanks for playing")