Black Jack Game In Python

  • Beat the dealer hand without going over 21.
  • Ace card represent 1 or 11 to make a better hand.
  • Face cards represent value of 10. Rest of the cards represent the number mentioned.
  • Hit means asking another card. Stand means to hold your current cards.
  • If anyone goes above 21, then they bust.
  • If you got 21 you bust, no matter what dealer gets.
  • If total of first 2 cards is 21, it’s a BLACKJACK.
  • Dealer needs to hit till he gets 17 or above.
  • If your card total and the dealer’s total is same, it’s a tie or Push.

Step 1: Import and Global Variables.

import random as r
from os import system

global num_of_decks
suits_dict = {'S': '\u2660', # Suits
'C': '\u2663', # Club
'H': '\u2665', # Heart
'D': '\u2666', # Diamond
'?': '?'} # Hidden card

Step 2: Creating a Card Class

Card class will be used to initialize the playing deck. As Blackjack can be played with one deck or multiple deck, this class will initiate with the number of deck selected by the user.

class Cards:
def __init__(self, nod):
# The Card Value
rank = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'A', 'J', 'Q', 'K']

# Type of Suit
suits = ['S', 'C', 'H', 'D']

# Creating a single deck
self.deck = ([(idx, jdx) for idx in suits for jdx in rank])

# Creating the number of deck as per user choice
self.deck = self.deck * nod

# Shuffle the deck
r.shuffle (self.deck)

def reshuffle_deck(self, nod):
input ("Deck about to finish. Reshuffling deck. Press any key to continue..")
self.__init__ (nod)

Step 3: Create a hand Class

Hand class will be used to hold the cards either by dealer or by the player. This will also hold the current chip balance, amount of chip betted by the player and whether the player won or lost. This Class will have multiple functions, each to be implemented on the hands deck.

class Hands:
def __init__(self, player_onhand=pow (2, 25), is_dealer=False):
# if this is a dealer hand
self.dealer_hand = is_dealer

# if this hand contains Ace card or total going above 21 for Ace card
self.ace = False = []
self.value = 0
self.player_current_balance = player_onhand = 0

def win_bet(self):
self.player_current_balance +=

def lose_bet(self):
self.player_current_balance -=

Step 4: Add a function to deal card

This function will take the last card out from the deck and add that card to the dealer or player hand. Also it will handle the Ace scenarios.

def add_card(self, card): (card)
if card[1] == 'A':
if not self.ace and (self.value + 11 <= 21):
self.value = self.value + 11
self.ace = True
self.value = self.value + 1
if type (card[1]) == int:
self.value = self.value + card[1]
self.value = self.value + 10
if self.value > 21 and self.ace:
self.value -= 10
self.ace = False

Step 5: Add function to design card

This function will design the card on the screen and it can be used to print each individual card.

def print_card(self, card_suit, pos=1):
s = ""
_ in card_suit:
s = s + "\t ________"
print (s.rjust (pos))
s = ""
idx, jdx in card_suit:
if jdx == 10:
s = s + "\t| {0} |".format (jdx)
s = s + "\t| {0} |".format (jdx)
print (s.rjust (pos))
s = ""
_ in card_suit:
s = s + "\t| |"
print (s.rjust (pos))
s = ""
idx, jdx in card_suit:
s = s + "\t| {0} |".format (suits_dict[idx])
print (s.rjust (pos))
s = ""
_ in card_suit:
s = s + "\t| |"
print (s.rjust (pos))
s = ""
idx, jdx in card_suit:
if jdx == 10:
s = s + "\t| {0} |".format (jdx)
s = s + "\t| {0} |".format (jdx)
print (s.rjust (pos))
s = ""
_ in card_suit:
s = s + "\t|________|"
print (s.rjust (pos))

Step 6: Function to Display the hand

This function can be used to display the current hand of the player or dealer.

def show_card(self, hide=False):
if self.dealer_hand:
if hide:
print ('Dealer'.rjust (60))
self.print_card ([[0], ('?', '?')])
print ('Dealer (Total: {0})'.format (self.value).rjust (60))
self.print_card (
for idx in range (4): print ('')
print ('Player (Total: {0})'.format (self.value).rjust (60))
self.print_card (
print ('Bet Amount: {0}'.format ( (60))

Step 7: Standalone function to Validate number

Validate whether the user is entering a number or a character.

def validate_number(p_text):
while True:
p_num = int (input (p_text))
except ValueError:
print ("Sorry, please enter a number")
return p_num

Step 8: Standalone function to take the bet

Create a function so that at the beginning of each game, player can place a bet.

def take_bet(player_hand):
while True: = validate_number ('How many chips you want to bet: ')
if > player_hand.player_current_balance:
print ("Sorry, you cannot bet more than {0}".format (player_hand.player_current_balance))
clear_screen ()

Step 9: Select the number of deck player wants to play with

def select_play_deck():
global num_of_decks
print ("How may decks you want to play with \n")
num_of_decks = validate_number ("Choose from number 1 to 4: ")
while num_of_decks not in range (1, 5):
clear_screen ()
num_of_decks = int (input ("Invalid Selection. Please select from number 1 to 4: "))
clear_screen ()

Step 10: Start the Game.

We will begin the game by asking the player to select the number of decks he wants to play with. This value will be used till the end of the game and every time we shuffle the cards, we are going to use this value.

print ('Welcome to Black Jack'.rjust (70))

# Asking the player to select the number of decks he would like to play with
select_play_deck ()

# Setting up the deck as per the user choice
card = Cards (num_of_decks)
# Setting up player opening balance to 100
player_opening_balance = 100

# Setting up the game flag to Y to start the game.
play = 'Y'
# Setting up Dealer hand
dealer = Hands (is_dealer=True)

# Setting up player hand with the number of chips he brought to table or he had after the last round
player = Hands (player_opening_balance)

# Taking bet from the player
take_bet (player)
# Dealing initial 2 cards each to player and dealer
for idx in range (2):
player.add_card (card.deck.pop ())
dealer.add_card (card.deck.pop ())

# Displaying dealer Card
dealer.show_card (True)
# Displaying Player Card
player.show_card ()
Displaying Player and Dealer Card
Player got busted
Player Stays at 21
Dealer Total more than 21. Dealer Busted.

My GitHub folder for the complete code:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ashu Anand

Ashu Anand

Developer, Engineer and an Analyst who is enthusiast enough to try new thing. Love to play sports and developing my Trading bot.