DEV Community


Posted on

My First Python Project (Blackjack)

Hey there.

I'm a newly minted lawyer who is already looking for something new. I've dabbled with coding a few times in the past. I even had professors in the past suggest that I switch my major to computer programming, but I didn't listen. But now I've decided to seriously pursue programming on my own time via Codecademy. This was the final project for the CS 101 course. My first "real" project took me a few hours and a lot of googling. Here it is!

Image description

The Code:

I used init for the individual card values, values, and suits. My name loops through 2 lists (suits and cards) and appends them to the deck list. It also uses the cards to navigates the card value dictionary.

Once the deck is generated the game begins.

The blackjack method starts by creating empty lists for the player and dealer hands, zeroing the participant scores, and stating that neither actor has a blackjack. The cards are dealt and it checks if anyone has a blackjack. If the player does it moves on to the dealer. If the dealer has a blackjack it waits to reveal it until the player has played. I also took care to adjust the value of an ace should it cause a bust. The player loops through the game hitting until he stands. If he busts the game will end on the spot.

I read that some casinos allow the dealer to chase the player even if the dealer has a 17 or more. So if the player has 20 the dealer will hit on 17 to try and get 20 or 21.

The end of the program makes a number of comparisons between the cards. If one player has a blackjack and the other does not, that player wins. If neither has a blackjack then the player with a greater score (that is still <= 21) wins!

If you have any critiques on it I would love to hear them!

Top comments (1)

danielsinoca profile image
Daniel Sinoca

Interesting implementation in Python, my blackjack article -