Building A Black Jack Advice Generator With Python

4 min read

I’ve been working on learning the in’s and out’s of Python in the last week and in this post want to share what I learned building a simple black jack advice generator. The gist of this project is to be able to tell the player what to do in the case of the three cards they currently have. So for example if the player is given an Ace, 5 and 7 should he or she hit or stay.

Python Dictionaries

The first thing we will need to do is build a dictionary that holds all the values for our cards. We will set the Ace to be equal to a value of 11 for the time being:

black_jack_advice.py
values = {
    '1': 1,
    '2': 2,
    '3': 3,
    '4': 4,
    '5': 5,
    '6': 6,
    '7': 7,
    '8': 8,
    '9': 9,
    '10': 10,
    'J': 10,
    'Q': 10,
    'K': 10,
    'A': 11
}

Dictionaries are similar to objects in JavaScript however they are a little different in how we access the data and I will admit I get yelled at by Python a lot of using dot notation which is no bueno.

black_jack_advice.py
my_dict = {
  "a": 1, # Note the key is a string a: 1 will not work in Python
  "b": 2
}
 
my_dict["a"] # 1
my_dict.a # Nastygram from Python

Now we will build two helper functions for later use in the main program. One will return the card value based on the supplied card and the other will sum the player’s cards.

black_jack_advice.py
def get_value(card):
    return values[card] # supply the string input from the player in bracket notation
 
 
def sum_values(a, b, c):
    return a + b + c

We can also build a helper function that will handle the logic of what advice to display to the player based on the sum of his or her cards. I’m using Python 3.6 so I’m using f-strings to do formatted printing.

black_jack_advice.py
def advice(total):
    if total < 17:
        print(f'Total is {total}, Hit!')
    elif total >= 17 and total < 21:
        print(f'Total is {total}, Stay!')
    elif total == 21:
        print(f'Total is {total}, Black Jack!')
    elif total > 21:
        print(f'Total is {total}, Busted!')

We can start putting together our main program and requesting input from the player.

black_jack_advice.py
def main():
    f = input('What is your first card? ')
    s = input('What is your second card? ')
    t = input('What is your third card? ')
    total = sum_values(
        get_value(f),
        get_value(s),
        get_value(t)
    )
    advice(total)
 
main()

The current code will work; however in Black Jack and Ace can be worth 1 or 11 based on the player’s current hand so how can we go about assigning the correct value to the player’s hand when they receive an ace instead of having a hard coded 11 as we do now?

We will build another helper method for this case called is_ace. Using boolean logic we will search the player’s hand first to see if and Ace is present in their hand and should they have an Ace check if the player’s total is greater than 21. If this conditional is full filled we will subtract 10 from the total (making the Ace’s value equal to 1).

black_jack_advice.py
def is_ace(cards, total):
    if 'A' in cards and total > 21:
        total -= 10
    return total

Now we can slip this helper function into our main program and we should get the correct advice according to the proper rules of black jack.

black_jack_advice.py
def main():
    f = input('What is your first card? ')
    s = input('What is your second card? ')
    t = input('What is your third card? ')
    total = sum_values(
        get_value(f),
        get_value(s),
        get_value(t)
    )
    total = is_ace([f, s, t], total)
    advice(total)
 
main()

Wrap Up

It wasn’t much but we learned how to use Python dictionaries to access key/value mappings and how to use boolean logic to handle the dynamic value of our player’s Ace card. I’m liking Python so far and can’t wait to learn more!

~ Cody 🚀

Related Articles


Cody Brunner

Cody is a Christian, USN Veteran, Jayhawk, and an American expat living outside of Bogotá, Colombia. He is currently looking for new opportunities in the tech industry.