4/3/2022»»Sunday

Blackjack In Python

4/3/2022
Blackjack In Python 8,7/10 4684 votes

I'm new to programming and I'm joining here to ask questions, contribute (when I have more knowledge under my belt), and basically just learn and figure out if programming is right for me.

  1. Coding Blackjack In Python
  2. How To Make Blackjack In Python
  3. Free 21 Plus 3 Blackjack
  4. Blackjack Free Game

I am currently learning Python through a course on Udemy and I'm working on a milestone project to create a Blackjack game. The only rule is for me to use Object Oriented Programming and to create classes for things like the Card and Deck.

Blackjack In Python

A standard deck of playing cards has four suits (Hearts, Diamonds, Spades and Clubs) and thirteen values (2 through 10, Jack, Queen, King and Ace) which makes a total of 52 cards per deck. Blackjack (Python recipe) by Mike McGowan. Text based command line blackjack. Hit and Stand are the only available options. Clean, commented code. Comment out any.

I've watched some tutorials on OOP, but I still do not feel confident in implementing them, so I was hoping I could share what I've written and hopefully have a better understanding through interactions here.

Learn Python - BlackJackIn this video: - Functions - Nested for loops - While loops - If statements - Comments - Welcome to Coding4Chicks -I am a prog.

To start, I created a plan to break the project down into smaller tasks as it was overwhelming to me. Here's what I planned:

  1. Create a deck with 52 cards

  2. Assign numerical values to each card (Ace can be 1 or 11)

  3. Shuffle the deck

  4. Dealer and player are handed 2 cards each as a start

  5. Player is asked if he/she wants to hit or stand

  6. Dealer has to hit until he reaches 17

  7. Compare 'points' to see who has more

  8. At any point, if anyone exceeds 21, that person loses

I am currently stuck at trying to figure out the first part. I'm basically trying to create a list with all 52 cards in it using OOP and for loops, but I can't seem to get things right.

I'm sharing my code below which was written in Python 2. Any ideas how to proceed?

Thank you,Paul

Edit #1: Thank you everyone for the comments and feedback. I think I am getting a little closer now. Can someone please let me know if I am doing this right and how I can print the list I used to create my deck such that I am able to see each of the 52 cards in a list? I tried using the str method but it appears I'm doing it wrongly.

In this challenge you are going to create a blackjack game for one player. The computer will be the dealer.

Rules of the game


Source: wikipedia.

“Blackjack, also known as twenty-one, is the most widely played casino banking game in the world. Blackjack is a comparing card game between a player and dealer, meaning that players compete against the dealer but not against any other players. It is played with one or more decks of 52 cards. The object of the game is to beat the dealer, which can be done in a number of ways:

  • Get 21 points on the player’s first two cards (called a blackjack), without a dealer blackjack;
  • Reach a final score higher than the dealer without exceeding 21; or
  • Let the dealer draw additional cards until his or her hand exceeds 21.

The player or players are dealt an initial two-card hand and add together the value of their cards. Face cards (kings, queens, and jacks) are counted as ten points. A player and the dealer can count his or her own ace as 1 point or 11 points. All other cards are counted as the numeric value shown on the card. After receiving their initial two cards, players have the option of getting a “hit”, or taking an additional card. In a given round, the player or the dealer wins by having a score of 21 or by having the highest score that is less than 21. Scoring higher than 21 (called “busting” or “going bust”) results in a loss. A player may win by having any final score equal to or less than 21 if the dealer busts. If a player holds an ace valued as 11, the hand is called “soft”, meaning that the player cannot go bust by taking an additional card; 11 plus the value of any other card will always be less than or equal to 21. Otherwise, the hand is “hard”.

Coding blackjack in python

The dealer has to take hits until his or her cards total 17 or more points. (In some casinos the dealer also hits on a “soft” 17, e.g. an initial ace and six.) Players win if they do not bust and have a total that is higher than the dealer’s. The dealer loses if he or she busts or has a lesser hand than the player who has not busted. If the player and dealer have the same total, this is called a “push” and the player typically does not win or lose money on that hand.”

Final Product


You can check this game online.

Your Challenge


Your challenge is to recreate this game for one player only using your chosen programming language (e.g. Python, JavaScript…). The end-user will play against the computer (the dealer).

To start with you may want to create this game using a text-based interface. Cards could be displayed as follows:

  • ♠ As (Ace of spade), 2s, 3s, … 9s, 10s, Js, Qs, Ks
  • ♥ Ah (Ace of heart), 2h, 3h, … 9h, 10h, Jh, Qh, Kh
  • ♣ Ac (Ace of club), 2c, 3c, … 9c, 10c, Jc, Qc, Kc
  • ♦ Ad (Ace of diamond), 2d, 3d, … 9d, 10d, Jd, Qd, Kd

You may also want to investigate unicode characters to display the selected cards in a more visual way.

Tips / Questions to get you started


Blackjack In PythonBefore getting started you may want to ask yourself the following questions?Python

Coding Blackjack In Python

  • What data structure could I use to create the deck of 52 cards?
  • Once I have stored all 52 cards in my data structure, how can I shuffle the cards?
  • What variables will I need to store the initial amount ($) the player has, the value of the bet ($), the total number of points of the dealer, the total number of points for the player?
  • Input: What information do I need to ask/retrieve from the end-user and when does the program need to ask for that information?
  • Output: What information do I need to display to the end-user during the game? (and when in the game?)
  • When or how does the game end?
  • How can I break up this project into smaller achievable steps? What would be the first few steps that I would then focus on?

Complete the Code…


Graphical User interface


Should you decide to create a full graphical user interface (e.g. as a webpage using HTML & JavaScript or as a Python program using graphical library such as PyGame) then you can download the graphics for each of the playing cards from wikimedia.

How To Make Blackjack In Python


Free 21 Plus 3 Blackjack


Other challenges you may enjoy...

Blackjack Free Game

Tagged with: iteration, List, Selection