A command-line Blackjack (21) game built in Python — play against the computer, decide when to hit or stand, and watch the dealer make its own (randomized) decisions. No GUI, no dependencies beyond the standard library — just clean control flow and card-game logic running straight in your terminal.
Type
yto deal in,nto walk away. Get as close to 21 as you can without going bust.
- 🃏 Randomized card dealing using
random.choices()from a weighted deck (face cards all valued at 10, Ace valued at 11). - 🧮 Automatic score calculation via
sum()on the player's and computer's hands. ♦️ Ace flexibility logic — if going over 21 with an Ace in hand, the Ace is swapped from 11 down to 1, just like real Blackjack rules.- 🤖 Simple computer opponent that randomly decides to hit or stand.
- 🔁 Replay loop — play as many rounds as you want without restarting the script.
- 🎨 ASCII art logo rendered at the start of each round for a nicer terminal experience.
- 🏆 Win / Lose / Draw logic covering busts, blackjacks (21), and score comparisons.
| Category | Tool / Concept |
|---|---|
| Language | Python 3 |
| Core Module | random (standard library) |
| Paradigm | Procedural / functional flow with a dedicated scoring function |
| Core Concepts | Conditional logic, randomization, loops, string formatting (f-strings) |
| Extras | ASCII art (art.py) for terminal presentation |
Blackjack Game/
│
├── blackjack.py # Entry point — game loop, dealing, and scoring logic
├── art.py # ASCII art logo displayed at the start of each round
└── README.md
Splitting the ASCII art into its own art.py module keeps blackjack.py focused purely on game logic — a small but tidy separation-of-concerns habit worth carrying into bigger projects.
- Python 3.x installed (no external packages required —
randomships with the standard library)
git clone https://github.com/VISHAL108-Mech/Blackjack.git
cd blackjack-game
python blackjack.py| Input | Action |
|---|---|
y |
Start a round / Hit (take another card) |
n |
Pass / Stand (keep your current hand) |
Instead of modeling individual suits, the deck is represented as a list of card values — Jack, Queen, and King are all flattened to 10, and the Ace is valued at 11 by default.
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]Each round, both the player and computer are dealt two cards using random.choices(), and only the computer's first card is revealed — mimicking the classic "dealer shows one card" rule.
player_cards = random.choices(cards, k=2)
player_score = sum(player_cards)
print(f"Your cards: {player_cards}\nYour current score: {player_score}")
computer_cards = random.choices(cards, k=2)
print(f"Computer first card: {computer_cards[0]}")The player chooses whether to draw another card. Based on that choice, the computer then makes its own random decision to hit or stand.
player_choice = input("Type 'y' to get another card, type 'n' to pass: ")
if player_choice == "y":
new_card = random.choice(cards)
player_cards.append(new_card)
player_score = sum(player_cards)
computer_choice = random.choice(["y", "n"])
if computer_choice != "n":
computer_cards.append(random.choice(cards))
calculate_score()
else:
calculate_score()If a hand busts (goes over 21) but contains an Ace counted as 11, the Ace is converted to 1 — the same "soft hand" rule used in real Blackjack.
elif 11 in player_cards and player_score > 21:
player_cards.remove(11)
player_cards.append(1)
elif 11 in computer_cards and computer_final_score > 21:
computer_cards.remove(11)
computer_cards.append(1)A dedicated calculate_score() function compares both hands and prints the outcome — covering draws, busts, blackjacks, and standard score comparisons.
def calculate_score():
"""Calculates the score of the player and computer based on their cards."""
print(f"Your final hand: {player_cards}\n"
f"Your final score: {player_score}")
computer_final_score = sum(computer_cards)
print(f"Computer final hand: {computer_cards}\n"
f"Computer final score: {computer_final_score}")
if player_score == computer_final_score:
print("Draw")
elif player_score > 21:
print("You went over. You Loose!")
elif computer_final_score > 21:
print("Opponent went over. You Win!")
elif player_score == 21:
print("You Win!")
elif computer_final_score == 21:
print("You Loose!")
elif player_score > computer_final_score:
print("You went over. You Loose!")
else:
print("You Loose!")The entire game runs inside a while run_game: loop, so the player is asked after every round whether they want to keep playing.
while run_game:
start_game = input("Do you want to play Blackjack? Type 'y' or 'n': ")
if start_game != "y":
run_game = False- Modeling real-world game rules (Ace flexibility, bust conditions, dealer behavior) as clean conditional logic
- Using
random.choices()vs.random.choice()correctly depending on whether multiple or single selections are needed - Structuring a program around a persistent game loop with clear exit conditions
- Separating presentation (ASCII art) from logic across multiple files
- Writing a self-contained scoring function that handles multiple game-ending scenarios
- Give the computer smarter hit/stand logic based on its own score (instead of pure randomness)
- Support multiple players
- Track wins/losses across a session
- Add betting/chip mechanics
- Add unit tests for
calculate_score()
VISHAL YADAV
- GitHub: https://github.com/VISHAL108-Mech
- LinkedIn: www.linkedin.com/in/vishal-yadav-2a91a7428
- Email: vy4122000@gmail.com