My first python project: Blackjack

Hey guys - I am new to coding and wanted to start making my own projects without as much supervision or direction. I made a Python script that can play blackjack on the user’s computer terminal and posted it to Github (

It essentially allows the user to: place a bet, hit/stay/double down on their move, and see which cards they are randomly drawn. The dealer doesn’t move if the player busts. The factors for bets are also considered (double down multiplies wins/losses by 2, natural blackjack pays out 3:2, and both together X3).

The card deck is a dictionary of card names (keys) matched to their score in Blackjack (values) with Aces having both a 1 and 11 value. The game is also able to keep track of whether an Ace should be “high” or “low” and can make adjustments during play.
The dealer is automated and does what every casino dealer does - hit until score is at least 17 and then stay.

I’d love if you guys can take a look at the script and let me know how I can make the code cleaner and better functioning. Feel free to run it and try your luck at the game, too! Thanks.

Data structures would be the first thing to use to simplify it.
You might for example have a Game which has a Dealer and Player(s), Deck(s)

A lot of logic can be split out into methods for those data structures. A Deck would know how to shuffle/draw, a Game might know what is currently in play and what bets there are, and it might also enforce rules and invoke the Dealer’s actions.
Every time something happens you might broadcast that as an Event, and anyone interested would be able to react to it.
You could for example create output based on events, and send input as events that the appropriate parts would listen to.

Mainly, what I would want to get away from is the “scriptlike” quality with loops that are bigger than the height of my monitor and instead have self-contained small units that can be understood separately from each other.

Don’t feel like you need to do any of it, it’s pretty overkill for the most part.

1 Like

I see… so potentially defining a “player” “dealer” and “card” class? That’s a really helpful thought for my next few projects! I really appreciate your feedback. I did have a feeling that my loops were getting frighteningly long!

They are, yeah. There are many ways you can go about it, the common goal for them would be to have smaller units of code so that you can look at each piece of it and make sense of it.

I think a good reference would be how you would explain the game, or, how you would manually play it, what actions or words do you need to do that? Implement those actions and words, and then describe the game the way you would do otherwise.

I haven’t thought things through but I imagine that various things would be interested in changes in others and that makes me think of events, whether it’s a single central event queue or for example letting different parts listen to whatever they might be interested in so that they get invoked at appropriate times. Alternatively, and perhaps a lot simpler, is to take some kind of pyramid approach to it. To break things down into subproblems like described in my previous paragraph.

Any indented block of code longer than a dozen lines or so could probably be its own function. Most pieces of code should be hugging the left side of the file as opposed to nesting further and further to the right side (not easy to keep track of)

Most “simplifications” would probably use much more language features than you are currently using. It’s not without cost. For a sufficiently small program, doing it all at once can work out just fine.

1 Like

This is great feedback, thank you! As you mentioned, I probably won’t bother in terms of this program, but going forward I will prioritize custom data structures to simplify loops and reduce massive numbers of conditional statements.
I can’t say enough how helpful it is to have better coders helping us visualize what we’re doing.

1 Like