import random import time # Function of the sum of all def total_hand(hand): total = 0 ace_count = 0 for card in hand: if card in ['J', 'Q', 'K']: total += 10 elif card == 'A': ace_count += 1 total += 11 else: total += int(card) while total > 21 and ace_count: total -= 10 ace_count -= 1 return total #Return the final hand value def deal_card(): cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] return random.choice(cards) #Main game def play_blackjack(): player_hand = [deal_card(), deal_card()] dealer_hand = [deal_card(), deal_card()] print("Your hand:", player_hand, "Total:", total_hand(player_hand)) print("Dealer's hand: [", dealer_hand[0], ", ?]") while total_hand(player_hand) < 21: move = input("Hit or Stand? (h/s): ").lower() if move == 'h': # Player chooses to hit player_hand.append(deal_card()) print("Your hand:", player_hand, "Total:", total_hand(player_hand)) else: # Player chooses to stand break player_total = total_hand(player_hand) if player_total > 21: print("Bust! You lose.") return print("Dealer's hand:", dealer_hand, "Total:", total_hand(dealer_hand)) while total_hand(dealer_hand) < 17: dealer_hand.append(deal_card()) print("Dealer draws:", dealer_hand, "Total:", total_hand(dealer_hand)) dealer_total = total_hand(dealer_hand) if dealer_total > 21 or player_total > dealer_total: print("You win!") elif player_total < dealer_total: print("Dealer wins!") else: print("It's a tie!") play_blackjack()