import random def play_blackjack(): suits = ['♠', '♣', '♥', '♦'] ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] deck = [f"{rank}{suit}" for rank in ranks for suit in suits] random.shuffle(deck) def calculate_score(hand): score = 0 aces = 0 for card in hand: rank = card[:-1] if rank in ['J', 'Q', 'K']: score += 10 elif rank == 'A': score += 11 aces += 1 else: score += int(rank) while score > 21 and aces: score -= 10 aces -= 1 return score # Deal initial cards player_hand = [deck.pop(), deck.pop()] dealer_hand = [deck.pop(), deck.pop()] # Player's Turn while True: p_score = calculate_score(player_hand) print(f"\nYour hand: {', '.join(player_hand)} (Score: {p_score})") print(f"Dealer shows: {dealer_hand[0]}") if p_score >= 21: break move = input("Do you want to [H]it or [S]tand? ").lower() if move == 'h': player_hand.append(deck.pop()) elif move == 's': break # Dealer's Turn p_score = calculate_score(player_hand) if p_score <= 21: print(f"\nDealer's hidden card was: {dealer_hand[1]}") while calculate_score(dealer_hand) < 17: print("Dealer hits...") dealer_hand.append(deck.pop()) print(f"Dealer's hand: {', '.join(dealer_hand)}") # Final Result d_score = calculate_score(dealer_hand) print(f"\nFinal Scores - You: {p_score}, Dealer: {d_score}") if p_score > 21: print("You busted! Dealer wins.") elif d_score > 21: print("Dealer busted! You win!") elif p_score > d_score: print("You win!") elif p_score < d_score: print("Dealer wins.") else: print("It's a push!") if __name__ == "__main__": while True: play_blackjack() if input("\nPlay another round? (y/n): ").lower() != 'y': break