import random # Function to simulate drawing a card (returns a value between 2 and 10) def card(): return random.randint(2, 10) # Function to calculate the hand's total value def calculate_hand(hand): return sum(hand) # Main game function def blackjack(): score = 0 play = input("Would you like to play BlackJack? (y/yes to start): ") if play == "y" or play == "yes": print("You draw 2 cards.") dealer_hand = [card(), card()] print(f"Dealer hand: {dealer_hand} Hand value;{calculate_hand(dealer_hand)} ") # Initial draw (2 cards) player_hand = [card(), card()] print(f"Your hand: {player_hand} | Hand value: {calculate_hand(player_hand)}") # Player's turn while calculate_hand(player_hand) and calculate_hand(dealer_hand) < 21: move = input(f"Do you want to 'hit' or 'stand'? ") if move == "hit": player_hand.append(card()) print(f"Your hand: {player_hand} | Hand value: {calculate_hand(player_hand)}") dealer_hand.append(card()) print(f"Dealer hand: {dealer_hand} Dealer value: {calculate_hand(dealer_hand)}") elif move == "stand": break else: print("Invalid input! Please choose 'hit' or 'stand'.") # Final score player_total == calculate_hand(player_hand) and dealer_total == calculate_hand(dealer_hand) if player_total > 21: print("You busted! Dealer wins.") elif dealer_total == 21: print("Dealer got 21! Better luck next time") elif player_total == 21: print("You got 21! YOU WIN") elif dealer_total > 21: print("Dealer busted! You win!") else: print("Maybe next time!") # Start the game blackjack()