You design a simple blackjack game in Python by separating the rules from the interface: represent cards as plain data, give each player a hand object that computes its best total, run a dealer loop that hits below 17, and let a text loop handle betting and choices. The key condition is using the standard casino rule that an ace counts as 11 or 1, whichever keeps the hand from busting. The version below uses a shuffled single deck, no splitting, and a dealer who stands on all 17s, which is the easiest complete ruleset to get right before you add complexity.

This walkthrough builds the whole game in one file, explains why each piece sits where it does, and flags the rule choices that quietly change who wins. Every code block is meant to be typed and run, not skimmed. By the end you'll have a working game and a clear map for extending it to multiple decks, splitting, or a graphical front end.

Decide the ruleset before you touch the keyboard

Blackjack looks like one game, but casinos and home tables vary in ways that alter the math. Pin down these decisions first so your code has a single source of truth:

Deck count. One 52-card deck is simplest. Casinos usually use six or eight decks, which changes card composition and slightly shifts probabilities. A single deck is fine for learning; note the choice in your code. Dealer behavior on soft 17. A "soft" 17 contains an ace counted as 11. Dealers either stand on all 17s or hit soft 17. Standing is easier and more beginner-friendly; hitting soft 17 makes the house stronger. Choose one and stick to it everywhere. Blackjack payout. A two-card 21 pays 3:2 at most tables. Some pay 6:5, which is worse for the player. Use 3:2 unless you have a reason. Splitting and doubling. Skip both in version one. They add state (multiple hands, extra bets) that obscures the core loop. Deck reshuffling. With one deck, reshuffle when it runs low. A common simple rule is to reset when fewer than 15 cards remain, or just expose the card count so the player sees what's left. Insurance. Skip it. It's a side bet with complicated edge cases and no place in a simple build.

Writing these down as constants at the top of your file—NUM_DECKS = 1, DEALER_STANDS_SOFT_17 = True, BLACKJACK_PAYOUT = 1.5—means changing a rule later is a one-line edit rather than a hunt through the code.

Represent cards and the shoe as plain data

A card needs a rank and a suit. A tuple is enough and keeps comparison logic explicit. The shoe is a list of cards that you shuffle once and pop from.

Why keep the ace as 11 in card_value? Because card value is a local property, while ace flexibility is a property of the hand. Mixing the two creates bugs the first time a hand holds two aces. The hand class below resolves the count.

Shuffling with random.shuffle is fine for a game; it is not cryptographically secure and should never drive anything with real money at stake. For a learning project, that distinction doesn't matter, but it's worth knowing.

Compute the best hand total with ace demotion

The classic beginner bug is adding aces as 11 and busting hands that should stand. The fix is simple: add every card at its base value with aces as 11, count how many aces you have, and demote them one at a time while the total exceeds 21.

Trace it with a hand of ace, six, ten. The raw total is 11 + 6 + 10 = 27, with one ace. The loop demotes the ace once, total becomes 17, and soft_aces drops to 0, so the hand is hard 17. With ace, ace, nine: raw 11 + 11 + 9 = 31, two aces; demote one to 21 hard? No—one demotion gives 21 with soft_aces = 1, so the hand is soft 21. That's correct: it's a 21 that can't bust by taking a card, because the remaining 11 could drop to 1.

The is_soft flag matters for the dealer rule. If your dealer stands on all 17s, soft or hard, you don't need the flag yet. If you later implement "hit soft 17," you do.

Structure the game as player, dealer, and table

Three concerns drive the whole game: each participant has a hand and a decision rule, the table holds the shoe and the bets, and the round logic sequences them. Keeping the participant logic in one place means the dealer and the player can share a add_card and total method.

The blackjack check needs both conditions: exactly two cards and a total of 21. A three-card 21 is not a natural and doesn't get the 3:2 bonus. This is a common place to introduce a rules bug—accepting any 21 as a blackjack pays the player too much.

The dealer is not a class of its own in a simple build; it's a Hand plus a loop in the round function. That keeps the code shorter without hiding logic.

Sequence a round correctly

A round has a fixed order: deal, resolve naturals, run the player's turn, run the dealer's turn, settle bets, then check the shoe. Getting the order wrong produces subtle errors—for example, paying a player blackjack before checking whether the dealer also has one.

The prerequisites: a shuffled shoe with at least a few cards, a bankroll, and a bet. Here are the numbered actions with checkpoints.

1: Check the shoe and reshuffle if low. Before dealing, if len(shoe) < 15, rebuild and shuffle. Success check: the shoe length resets. 2: Take the bet and validate it. Reject bets above the bankroll or below the table minimum. Deduct the bet from the bankroll immediately; winnings get added back on settlement. Keeping the bet "out" until resolution prevents paying a player who already spent their stake. 3: Deal two cards to the player and two to the dealer, alternating, one of the dealer's cards face down. In a text game, print only the dealer's upcard during the player's turn. 4: Check for naturals. If the player has a blackjack and the dealer's upcard isn't an ace or ten-value card, pay 3:2 immediately. If the dealer's upcard is an ace or ten, the player's turn is effectively over—reveal the dealer's hole card and settle. A player blackjack against dealer blackjack is a push. 5: Player's turn. Loop: show the hand, ask hit or stand. If the total passes 21, stop and settle as a loss. On stand, exit the loop. 6: Dealer's turn. Reveal the hole card. While the dealer's total is below 17, or equals 17 and the rule says hit soft 17, add a card. Stop if the dealer busts. 7: Settle. Compare totals: dealer bust means player wins; player bust already lost; higher total wins; equal totals push. 8: Report bankroll and ask whether to play another round.

A version of steps 3–7 as code:

That last line is a deliberate trap—notice that bet * 2 needs to return the original stake plus the win, which is bankroll + bet * 2, not bankroll + bet. But the snippet above has a typo to illustrate a point: any copy-paste of logic this dense should be retyped or tested line by line, not trusted. A corrected settlement block follows.

Check the arithmetic: after the bet is deducted, a win returns the stake plus an equal win, so + bet * 2 gets the player back to even plus profit. A blackjack at 3:2 returns the stake plus 1.5× the stake, so + bet * 2.5. A push returns the stake, + bet. A loss returns nothing extra, + 0. If any of those look wrong, write the bankroll before and after a hand on paper—this is the single easiest place to lose money in the simulation.

Wire the main loop and handle player input

The outer loop owns the bankroll and the shoe, and keeps going until the player quits or goes broke.

Two details are worth defending. First, validating the bet before deducting prevents a negative bankroll. Second, checking bankroll >= 1 in the loop condition means a player who loses everything exits gracefully rather than being asked for a bet they can't cover. If your table minimum is 5, change the condition to match.

A working success check for the whole program: play ten hands manually and confirm that (a) a player natural pays 1.5× the bet, (b) a push returns exactly the stake, (c) a dealer bust pays 1:1, and (d) the bankroll prints match a hand calculation. If any of those fail, the settlement block is almost certainly the culprit.

Test the rules that break naive implementations

Manual play catches most bugs, but three rules deserve automated checks because they're easy to get wrong and hard to spot by eye.

Ace demotion. A hand of A, A, 9 should be 21 soft, not bust. A hand of A, A, A, 8 should be 21 hard? Let's verify: 11 + 11 + 11 + 8 = 41; demote two aces to get 21, leaving one ace still at 11, so it's soft 21. If your test says bust, the loop is demoting only one ace. Write a small function that iterates over known hands and asserts the expected totals—this is the highest-value ten lines of code in the project.

Dealer soft 17. A dealer hand of A, 6 is 17 soft. If you chose "hit soft 17," the dealer takes another card; if the next card is a 10, the hand becomes 27 and busts (after demotion, 17 hard, but the dealer already drew past the threshold—check your loop order). The loop condition needs the soft check to read the current total, not a cached value.

Natural resolution order. A player natural against a dealer upcard of 10 or A must not be paid before the dealer's hole card is checked. The code above handles this by falling through to dealer resolution, which is correct but only if the dealer's hidden card is inspected before settlement. A test that forces a player natural and a dealer natural in the same round should produce a push.

For these, use random.seed and construct hands directly rather than dealing from a shuffled shoe—it makes the test deterministic and readable.

Extend the game without breaking the core

The design above deliberately keeps the rules in one function and the data in simple objects, so extensions are additive rather than invasive. A few obvious next steps, in rough order of difficulty:

Multiple decks is a one-line change—pass NUM_DECKS = 6—though you should re-shuffle based on a penetration target (say, 75% of the shoe) rather than a fixed 15-card threshold. The single-deck threshold of 15 cards is an arbitrary convenience; with six decks, a 15-card cutoff would trigger reshuffles far too often.

Doubling down means allowing one extra card at double the original bet, with the rule usually restricted to the first two cards. You'd need to track whether the player has taken a card and whether doubling is still legal. The settlement then uses the doubled bet.

Splitting is the big one. When two cards of the same rank are dealt, the player can split them into two hands, each with its own bet. This requires the dealer to compare against each hand and the settlement to loop. Aces split usually get one card each and no further hits. Skip splitting until the single-hand version passes tests.

A graphical interface would replace the input/print loop with widgets but leave the Hand, hand_total, and settlement functions untouched—that's the payoff of keeping I/O out of the rules code. If you find yourself re-implementing ace counting inside a button handler, the abstraction leaked.

One extension to avoid: a "player hints" feature that tells the player the mathematically optimal move. Basic strategy depends on the dealer's upcard, the player's total, softness, and whether doubling or splitting is legal, so a hint system is a research-grade side project on its own and doesn't belong in a first version.

Where simple blackjack simulations go wrong

Beyond the settlement arithmetic, a few structural mistakes recur in beginner blackjack projects.

Shuffling after every deal. Some tutorials rebuild and shuffle the shoe each hand. That turns the game into an independent-draw model, which is fine for a toy but misleading if you later reason about card counting or deck composition. Keep one shoe and pop from it.

Treating aces as always 11. This is the demotion bug in disguise, and it surfaces as frequent unwinnable hands. The hand_total function above is the antidote; test it.

Mixing display and logic. Printing from inside hand_total or the settlement function makes testing harder and the code harder to read. Keep the display in the play_round and main functions and return values everywhere else.

Assuming a 21 is a blackjack. A three-card 21 pays even money, not 3:2. The is_blackjack check with its two-card condition prevents an overpay that would otherwise go unnoticed in casual play.

Ignoring bankroll edge cases. A player who bets their full bankroll and wins must not be paid twice. The deduct-then-add pattern here is the simplest guard.

None of these are exotic. They're the bugs that show up in the first hour of testing, and the structure above is arranged to make each one easy to spot and fix.

FAQ

Should the dealer's hole card be dealt before the player's turn or only when the dealer plays? Standard casino procedure deals both dealer cards up front, one face down. Either implementation is fine for a text game, but dealing the hole card up front means your shoe has one fewer card during the player's turn, which matters if you later track card composition. Keep the up-front deal.

How do I handle a player who has exactly 21 with three cards against a dealer's blackjack? The dealer's natural beats any 21 that is not itself a natural, so the player loses. The settlement block above checks naturals before comparing totals, which handles this correctly—but only if player_natural is computed with the two-card condition.

Is a single-deck game actually simpler than a multi-deck game? The code is barely different—multi-deck is a constant change. The real complexity is the reshuffle policy, which is easier to reason about with one deck but should still use a penetration threshold rather than a fixed card count if you want realistic behavior.

What about the "dealer peeks for blackjack" rule? Some tables have the dealer check the hole card for a natural when the upcard is a 10 or ace, and the player's turn is skipped if the dealer has one. The code above approximates this by resolving naturals first. If you want exact table behavior, add an explicit peek step before the player's turn when the upcard is 10 or A, and skip the player's actions if the dealer has a natural.