Blackjack console game using Python

Last Updated : 18 Apr, 2026

Blackjack, also known as Twenty-One, is a popular card game played between a player and a dealer. The objective of the game is to get a total card value as close to 21 as possible without exceeding it. This game is a great example to understand how basic Python concepts like lists, loops, functions, and conditional statements work together.

Rules of Blackjack Game

Let's first understand the game's rules before writing the blackjack console game code.

  1. Each player and the dealer get two cards initially.
  2. The player can choose to "play" (take another card) or "stop" (hold the current score).
  3. Face cards (Jack, Queen, King) are worth 10 points.
  4. Aces can be worth 11 points.
  5. Exceeding 21 points results in an automatic loss.
  6. The winner is the player with the highest score ≤ 21 after comparing with the dealer.

Step-by-Step Implementation

Step 1. Import Required Module: The random module is used to shuffle the deck of cards.

Python
import random

Step 2. Create a Deck of Cards: Create a list of cards and suits, then combine them as tuples for the full deck.

Python
card_categories = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
cards_list = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
deck = [(card, category) for category in card_categories for card in cards_list]

Step 3. Define Card Values: The below function assigns points to each card.

Python
def card_value(card):
    if card[0] in ['Jack', 'Queen', 'King']:
        return 10
    elif card[0] == 'Ace':
        return 11
    else:
        return int(card[0])

Step 4. Shuffle the Deck and Deal Initial Cards: Shuffle the deck and deal two cards each to the player and dealer.

Python
random.shuffle(deck)
player_card = [deck.pop(), deck.pop()]
dealer_card = [deck.pop(), deck.pop()]

Step 5. Player’s Turn: The player can hit (draw a card) or stand (stop) until they stop or exceed 21 points.

Python
while True:
    player_score = sum(card_value(card) for card in player_card)
    dealer_score = sum(card_value(card) for card in dealer_card)
    print("Cards Player Has:", player_card)
    print("Score Of The Player:", player_score)
    print("Dealer's Visible Card:", dealer_card[0])  # only one card shown
    print("\n")

    choice = input('What do you want? ["play" to request another card, "stop" to stop]: ').lower()
    if choice == "play":
        new_card = deck.pop()
        player_card.append(new_card)
    elif choice == "stop":
        break
    else:
        print("Invalid choice. Please try again.")
        continue

    if player_score > 21:
        print("Cards Dealer Has:", dealer_card)
        print("Score Of The Dealer:", dealer_score)
        print("Cards Player Has:", player_card)
        print("Score Of The Player:", player_score)
        print("Dealer wins (Player Loss Because Player Score is exceeding 21)")
        break

Step 6. Dealer’s Turn: The dealer draws cards until reaching 17 or higher.

Python
while dealer_score < 17:
    new_card = deck.pop()
    dealer_card.append(new_card)
    dealer_score += card_value(new_card)

print("Dealer reveals cards:", dealer_card)
print("Dealer's Score:", dealer_score)
print("\n")

Step 7. Determine the Winner: Compare scores to determine the winner.

Python
if dealer_score > 21:
    print("Player wins (Dealer Loss Because Dealer Score is exceeding 21)")
elif player_score > dealer_score:
    print("Player wins (Player Has Higher Score than Dealer)")
elif dealer_score > player_score:
    print("Dealer wins (Dealer Has Higher Score than Player)")
else:
    print("It's a tie.")
    
print("Final Cards - Player:", player_card, "Score:", player_score)
print("Final Cards - Dealer:", dealer_card, "Score:", dealer_score)

Complete Code

Python
import random

card_categories = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
cards_list = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
deck = [(card, category) for category in card_categories for card in cards_list]

def card_value(card):
    if card[0] in ['Jack', 'Queen', 'King']:
        return 10
    elif card[0] == 'Ace':
        return 11
    else:
        return int(card[0])

random.shuffle(deck)
player_card = [deck.pop(), deck.pop()]
dealer_card = [deck.pop(), deck.pop()]

while True:
    player_score = sum(card_value(card) for card in player_card)
    dealer_score = sum(card_value(card) for card in dealer_card)
    print("Cards Player Has:", player_card)
    print("Score Of The Player:", player_score)
    print("Dealer's Visible Card:", dealer_card[0])  
    print("\n")

    choice = input('What do you want? ["play" to request another card, "stop" to stop]: ').lower()
    if choice == "play":
        new_card = deck.pop()
        player_card.append(new_card)
    elif choice == "stop":
        break
    else:
        print("Invalid choice. Please try again.")
        continue

    if player_score > 21:
        print("Cards Dealer Has:", dealer_card)
        print("Score Of The Dealer:", dealer_score)
        print("Cards Player Has:", player_card)
        print("Score Of The Player:", player_score)
        print("Dealer wins (Player Loss Because Player Score is exceeding 21)")
        break

while dealer_score < 17:
    new_card = deck.pop()
    dealer_card.append(new_card)
    dealer_score += card_value(new_card)

print("Dealer reveals cards:", dealer_card)
print("Dealer's Score:", dealer_score)
print("\n")

if dealer_score > 21:
    print("Player wins (Dealer Loss Because Dealer Score is exceeding 21)")
elif player_score > dealer_score:
    print("Player wins (Player Has Higher Score than Dealer)")
elif dealer_score > player_score:
    print("Dealer wins (Dealer Has Higher Score than Player)")
else:
    print("It's a tie.")
    
print("Final Cards - Player:", player_card, "Score:", player_score)
print("Final Cards - Dealer:", dealer_card, "Score:", dealer_score)

Output

output
Comment