# Untitled

unknown
plain_text
a year ago
2.7 kB
1
Indexable
Never
```import random

# Create a deck of cards
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck = []

# Create a card deck
for suit in suits:
for rank in ranks:
deck.append(rank + ' of ' + suit)

# Function to calculate the value of a hand
def calculate_hand_value(hand):
value = 0
num_aces = 0

# Iterate through each card in the hand
for card in hand:
if card[0].isdigit():
value += int(card[0])
elif card[0] in ['J', 'Q', 'K']:
value += 10
elif card[0] == 'A':
value += 11
num_aces += 1

# Adjust the value if the hand contains aces
while value > 21 and num_aces > 0:
value -= 10
num_aces -= 1

return value

# Shuffle the deck
random.shuffle(deck)

# Deal two initial cards to the player and dealer
player_hand = [deck.pop(), deck.pop()]
dealer_hand = [deck.pop(), deck.pop()]

# Game loop
while True:
# Display player's hand and value
print("Player's Hand:", player_hand)
print("Player's Hand Value:", calculate_hand_value(player_hand))

# Display dealer's hand (showing only one card)
print("Dealer's Hand:", [dealer_hand[0]])

# Check if player or dealer has blackjack
if calculate_hand_value(player_hand) == 21:
print("Player wins with a Blackjack!")
break
elif calculate_hand_value(dealer_hand) == 21:
print("Dealer wins with a Blackjack!")
break

# Ask the player to hit or stand
choice = input("Do you want to hit or stand? (h/s): ")

# Player chooses to hit
if choice.lower() == 'h':
player_hand.append(deck.pop())

# Check if player busts
if calculate_hand_value(player_hand) > 21:
print("Player busts. Dealer wins!")
break
# Player chooses to stand
elif choice.lower() == 's':
# Dealer's turn to draw cards
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deck.pop())

# Display dealer's hand and value
print("Dealer's Hand:", dealer_hand)
print("Dealer's Hand Value:", calculate_hand_value(dealer_hand))

# Determine the winner
player_value = calculate_hand_value(player_hand)
dealer_value = calculate_hand_value(dealer_hand)

if player_value > dealer_value:
print("Player wins!")
elif player_value < dealer_value:
print("Dealer wins!")
else:
print("It's a tie!")

break
else: