import random def draw_card(): """Draw a random card from the deck.""" cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] return random.choice(cards) def calculate_score(hand): """Calculate the total score of a hand.""" if sum(hand) == 21 and len(hand) == 2: return 0 # Blackjack if 11 in hand and sum(hand) > 21: hand.remove(11) hand.append(1) return sum(hand) def play_game(): player_hand = [draw_card(), draw_card()] computer_hand = [draw_card(), draw_card()] game_over = False while not game_over: player_score = calculate_score(player_hand) computer_score = calculate_score(computer_hand) print(f"Your cards: {player_hand}, current score: {player_score}") print(f"Computer's first card: {computer_hand[0]}") if player_score == 0 or computer_score == 0 or player_score > 21: game_over = True else: should_continue = input("Type 'y' to get another card, 'n' to pass: ") if should_continue.lower() == 'y': player_hand.append(draw_card()) else: game_over = True while computer_score != 0 and computer_score < 17: computer_hand.append(draw_card()) computer_score = calculate_score(computer_hand) print(f"Your final hand: {player_hand}, final score: {player_score}") print(f"Computer's final hand: {computer_hand}, final score: {computer_score}") determine_winner(player_score, computer_score) def determine_winner(player_score, computer_score): """Determine the winner of the game.""" if player_score > 21: print("You went over. You lose.") elif computer_score > 21: print("Computer went over. You win!") elif player_score == computer_score: print("It's a draw!") elif player_score == 0: print("You got a Blackjack! You win!") elif computer_score == 0: print("Computer got a Blackjack. You lose.") elif player_score > computer_score: print("You win!") else: print("You lose.") # Start the game play_game()