New to coding: need help identifing issue in basic game

Hey everyone! I’m attempting to learn Python and I am working on making a simple black jack game. It’s still in progress, but what I have completed should be able to give you two cards to start with another card given each time the user inputs “Y” when prompted “Hit?” It should keep a running total value of cards, with face cards equal to 10 except for aces which should equal 11 unless the total goes over 21 and then their value changes to 1. The issue I am having is that the displayed total is sometimes right and sometimes not when I run the program and I can’t find what I’m doing wrong. Can someone with more experience help me solve this?
Here’s what I have so far…

Please post your code in text form, as explained in the pinned post.

What exactly happens when you draw a face card? Let’s say a Queen (represented in your code with a 12). Walk through the code when this happens, and see if you can spot what’s going on.

1 Like

My mistake. Everything that I can see lines up with what should occur- the Queen becomes a value of 10 added to T and the program continues, but it sometimes the total runs right, sometimes low, sometimes high.

c = 0 #card
A = 0 #number of aces
H = “Y” #hit yes or no
T = 0 #running total value of hand

import random
c = (random.randint(1,13))
if c < 11 and c > 1:
print (c)
T = (c + T)
elif c == 1:
print (“Ace”)
T = (c + 11)
A = (A + 1)
elif c == 11:
print (“Jack”)
T = (c + 10)
elif c == 12:
print (“Queen”)
T = (c + 10)
elif c == 13:
print (“King”)
T = (c + 10)
while H == (“Y”):
import random
c = (random.randint(1,13))
if c < 11 and c > 1:
print (c)
T = (c + T)
elif c == 1:
print (“Ace”)
T = (c + 11)
A = (A + 1)
elif c == 11:
print (“Jack”)
T = (c + 10)
elif c == 12:
print (“Queen”)
T = (c + 10)
elif c == 13:
print (“King”)
T = (c + 10)
if T > 21 and A > 0:
A = (A-1)
T = (T-10)
total = str (T)
print ("Total: " , total)
H = input('Hit? Y or N? ')

1 Like

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

Have you considered that there might be more than 1 ace?

1 Like
Sorry again! Thanks for your patience. Yes, I considered the possibility of multiple aces, which is why I included 
        if T > 21 and A > 0:
                A = (A-1)
                T = (T-10)

Anyway, hopefully this works:

c = 0 #card
A = 0 #number of aces
H = "Y" #hit yes or no
T = 0 #running total value of hand

import random
c = (random.randint(1,13))
if c < 11 and c > 1:
        print (c)
        T = (c + T)
elif c == 1:
        print ("Ace")
        T = (c + 11)
        A = (A + 1)
elif  c == 11:
        print ("Jack")
        T = (c + 10)
elif c == 12:
        print ("Queen")
        T = (c + 10)
elif c == 13:
        print ("King")
        T = (c + 10)
while H == ("Y"):
        import random
        c = (random.randint(1,13))
        if c < 11 and c > 1:
                print (c)
                T = (c + T)
        elif c == 1:
                print ("Ace")
                T = (c + 11)
                A = (A + 1)
        elif  c == 11:
                print ("Jack")
                T = (c + 10)
        elif c == 12:
                print ("Queen")
                T = (c + 10)
        elif c == 13:
                print ("King")
                T = (c + 10)
        if T > 21 and A > 0:
                A = (A-1)
                T = (T-10)
        total = str (T)
        print ("Total: " , total)
        H = input('Hit? Y or N? ')

What does this last line do?

You wrote “… aces which should equal 11 unless the total goes over 21 and then their value changes to 1.”.

Consider this scenario.

The play gets an ace, so A == 1 and T == 11.

Then the player gets another ace, so A == 2 and T == 22.

The code:

    if T > 21 and A > 0:
        A = (A-1)
        T = (T-10)

will reduce A to 1 and T to 12.

The play will then be asked whether to hit.

Is that intentional?

yes, if they go over 21 again, they will have the ability to reduce the second ace to 1.

but I found the problem!! I had some “c” where there should have been “T” Here is the corrected code:

c = 0 #card
A = 0 #number of aces
H = "Y" #hit yes or no
T = 0 #running total value of hand
import random
c = (random.randint(1,13))
if c < 11 and c > 1:
        print (c)
        T = c
elif c == 1:
        print ("Ace")
        T = 11
        A = 1
elif  c == 11:
        print ("Jack")
        T = 10
elif c == 12:
        print ("Queen")
        T = 10
elif c == 13:
        print ("King")
        T = 10
while H == ("Y"):
        import random
        c = (random.randint(1,13))
        if c < 11 and c > 1:
                print (c)
                T = (c + T)
        elif c == 1:
                print ("Ace")
                T = (T + 11)
                A = (A + 1)
        elif  c == 11:
                print ("Jack")
                T = (T + 10)
        elif c == 12:
                print ("Queen")
                T = (T + 10)
        elif c == 13:
                print ("King")
                T = (T + 10)
        if T > 21 and A > 0:
                A = (A-1)
                T = (T-10)
        total = str (T)
        print ("Total: " , total)
        H = input('Hit? Y or N? ')
1 Like

Thanks everyone for your replies!! On to finish the code now

1 Like

Bullseye :slight_smile:

1 Like