# Blackjack Simulator

## Python Game project

Blackjack is one of the most popular card games played in Casinos worldwide. The game is also known as 21 because the primary objective is to reach a hand value of 21 or as close to it as possible without exceeding it.

The goal of this Python game project is to code a simplified version of blackjack. You must write a program to check the winner of blackjack.

The input of the program will be:

1) Number and name of players. The program will prompt the user to enter the number and name of the players. For this project assume there are always less than 4.

2) Cards of each player. The program will create a function that assigns randomly to each player Â 4 cards. The cards of a standard deck are:
Ace,2,3,4,5,6,7,8,9,10,J,Q,K

• Numbered cards are worth their number. For example 6 is worth 6.
• Face cards (J,Q,K,) are worth 10.
• Ace can be worth 1 or 11

The program must compare the cards of all the players and calculate the winner. The winner is the player with the higher value near 21. If it is greater than 21 it automatically loses. The program should print out the name of the winner and the cards of all players. If all players exceed 21, then there is no winner

### Expected output

```Enter the number of players (less than 4): 3
Enter name of player 1: David
Enter name of player 2: Maria
Enter name of player 3: Luc
Player Cards:
David: ['3', '2', 'J', 'Q']
Maria: ['3', 'Q', 'K', 'A']
Luc: ['8', '5', 'J', '5']
No winner
```

### Source code

``````
import random

# Function to assign cards to each player
def assign_cards(players):
cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
player_cards = {}
for player in players:
player_cards[player] = [random.choice(cards) for _ in range(4)]
return player_cards

# Function to calculate the total value of the cards
def calculate_total_value(cards):
total = 0
aces = 0
for card in cards:
if card.isdigit():
total += int(card)
elif card in ['J', 'Q', 'K']:
total += 10
elif card == 'A':
aces += 1
total += 11
while total > 21 and aces:
total -= 10
aces -= 1

# Input number of players
num_players = int(input("Enter the number of players (less than 4): "))

# Input player names
players = []
for i in range(num_players):
player_name = input(f"Enter name of player {i+1}: ")
players.append(player_name)

# Assign cards to each player
player_cards = assign_cards(players)

# Calculate total value of cards for each player
player_values = {}
for player, cards in player_cards.items():
total_value = calculate_total_value(cards)
player_values[player] = total_value

# Find the winner
winning_value = 0
winner = ""
for player, value in player_values.items():
if value > winning_value and value <= 21:
winning_value = value
winner = player

# Print the results
print("Player Cards:")
for player, cards in player_cards.items():
print(f"{player}: {cards}")
print(f"The winner is: {winner} with a total value of {winning_value}" if winner else "No winner")

``````

image

#### Practity

We will be happy to hear your thoughts