Write a Program to create BlackJack Game in Python.
Here is a step by step guide to creating a Blackjack game in Python.
- Program to Create Snake Game in Python
- Write a Program to Create Hangman Game in Python
- Program to Create Tic Tac Toe Game in Python
Step 1: Game Setup

First we’ll import the necessary libraries – we’ll be using the random
library for shuffling the deck. We’ll also define a few variables to represent the game state, including deck
, which represents the cards in the deck, and suits
and ranks
, which are arrays of all possible suits and ranks in a standard deck of cards.
import random
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
deck = []
Step 2: Creating the Deck
To create the deck we’ll loop through all possible combinations of suits and ranks and append each card to the deck array.
for suit in suits:
for rank in ranks:
deck.append((rank, suit))
Step 3: Shuffling the Deck
Before each round of Blackjack should be shuffled so that the cards are in a random order. We can use the random.shuffle()
function to shuffle the deck in place.
random.shuffle(deck)
Step 4: Dealing the Cards
In Blackjack player and dealer are each dealt two cards. We can represent the player and dealer hands using arrays.
player_hand = []
dealer_hand = []
To deal the cards we’ll simply pop the top two cards off the deck and append them to the player and dealer hands.
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
Step 5: Calculating Hand Value
The value of a Blackjack hand is the sum of the point values of each individual card. In our game we’ll use the following point values:
- 2-10: face value
- Jack, Queen, King: 10
- Ace: 1 or 11, depending on the value of the other cards in the hand
To calculate the value of a hand we can loop through each card in the hand and add its point value to a running total. We’ll also keep track of the number of aces in the hand so that we can adjust their value if necessary.
def calculate_hand_value(hand):
value = 0
num_aces = 0
for card in hand:
rank = card[0]
if rank.isnumeric():
value += int(rank)
elif rank == 'Ace':
num_aces += 1
value += 11
else:
value += 10
while value > 21 and num_aces > 0:
value -= 10
num_aces -= 1
return value
Step 6: Displaying the Hands
To display the player and dealer hands we can use a simple print statement.
print("Player's Hand:", player_hand)
print("Dealer's Hand:", dealer_hand[0], 'and [Hidden Card]')
Step 7: Player Turn
During the player’s turn they are given the option to hit (take another card) or stand (end their turn with their current hand). We’ll use a simple loop to repeat the player turn until they stand or bust.
while True:
action = input("Do you want to Hit or Stand? ")
if action == 'Hit':
player_hand.append(deck.pop())
print("Player's Hand:", player_hand)
if calculate_hand_value(player_hand) > 21:
print("Bust! You lose.")
return -1
elif action == 'Stand':
break
else:
print("Invalid action. Please enter Hit or Stand.")
If the player chooses to hit we’ll append a new card to their hand and check if they have gone over 21 (i.e busted). If the player busts game ends and player loses. If the player chooses to stand, their turn is over.
Step 8: Dealer Turn
After the player’s turn it’s the dealer’s turn to play. The dealer must continue to hit until their hand value is at least 17.
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deck.pop())
print("Dealer's Hand:", dealer_hand)
if calculate_hand_value(dealer_hand) > 21:
print("Dealer busts! You win.")
return 1
If dealer busts player wins. If not we move on to the final step.
Step 9: Determine the Winner
To determine the winner we compare hand values of player and dealer. If the player’s hand value is higher they win. If the dealer’s hand value is higher they win. If there’s a tie game is a push.
player_value = calculate_hand_value(player_hand)
dealer_value = calculate_hand_value(dealer_hand)
if player_value > dealer_value:
print("You win!")
return 1
elif dealer_value > player_value:
print("Dealer wins!")
return -1
else:
print("Push! The game is a tie.")
return 0
Final Code
Here is complete code for Blackjack game in python:
import random
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
deck = []
for suit in suits:
for rank in ranks:
deck.append((rank, suit))
random.shuffle(deck)
player_hand = []
dealer_hand = []
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
def calculate_hand_value(hand):
value = 0
num_aces = 0
for card in hand:
rank = card[0]
if rank.isnumeric():
value += int(rank)
elif rank == 'Ace':
num_aces += 1
value += 11
else:
value += 10
while value > 21 and num_aces > 0:
value -= 10
num_aces -= 1
return value
print("Player's Hand:", player_hand)
print("Dealer's Hand:", dealer_hand[0], 'and [Hidden Card]')
while True:
action = input("Do you want to Hit or Stand? ")
if action == 'Hit':
player_hand.append(deck.pop())
print("Player's Hand:", player_hand)
if calculate_hand_value(player_hand) > 21:
print("Bust! You lose.")
break
elif action == 'Stand':
break
else:
print("Invalid action. Please enter Hit or Stand.")
if calculate_hand_value(player_hand) <= 21:
print("Dealer's Hand:", dealer_hand)
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deck.pop())
print("Dealer's Hand:", dealer_hand)
if calculate_hand_value(dealer_hand) > 21:
print("Dealer busts! You win.")
break
player_value = calculate_hand_value(player_hand)
dealer_value = calculate_hand_value(dealer_hand)
if player_value > dealer_value:
print("You win!")
elif dealer_value > player_value:
print("Dealer wins!")
else:
print("Push! The game is a tie.")
We first define the card suits and ranks and create a list deck
to hold all possible cards. We then shuffle deck.
We initialize player and dealer hands with two cards each. We also define a calculate_hand_value
function that takes a hand and returns its total value.
During the game we display the player’s hand and the dealer’s hand (with one card hidden). The player is given the option to hit or stand. If they hit we append a new card to their hand and check if they have gone over 21 (i.e. busted). If the player busts the game ends and the player loses. If the player stands their turn is over.
If the player didn’t bust we reveal the dealer’s hand and let the dealer hit until their hand value is at least 17. If the dealer busts, the player wins. If not we compare the hand values of the player and dealer to determine the winner.
Overall this is a simple implementation of the Blackjack game in Python. You could add more features and gameplay options to make the game more interesting and engaging.