Addy
Addy

Reputation: 105

why does python variable not show up in certain situations

i was trying to make blackjack in python but it get does not work as intended when the computer wins and i can not see why. here is my code:

import random

hand = []
chand = []
bust = 'no'
sit = 'no'
value = 0
ais = 0
play = 'yes'
x = 0
cards = {
        'a': 11,
        '2': 2,
        '3': 3, 
        '4': 4,
        '5': 5, 
        '6': 6, 
        '7': 7,
        '8': 8,
        '9': 9,
        '10': 10,
        'j': 10,
        'q': 10,
        'k': 10, 
        }



print('''welcome to black jack
type draw or sit to get started''')
while x < 2:
        cardc = random.choice(list(cards.keys()))
        hand.append(cardc)
        x = x + 1
print('your cards', ', '.join(hand))
while bust != 'yes' and sit != 'yes':
        player = input('what would you like to do: ')
        if player == 'draw':
                cardc = random.choice(list(cards.keys()))
                hand.append(cardc)
                print('your cards', ', '.join(hand))
                value = 0
                for i in hand:
                        value = value + cards[i]
#                print(value)
                if value > 21:
                        bust = 'yes'
        elif 'sit' in player:
                print('you have sat')
                sit = 'yes'
if 'yes' in bust:
        print ('you lose')



if sit == 'yes':
        while ais < value and ais < 21:
                cardc = random.choice(list(cards.keys()))
                chand.append(cardc)
                ais = 0
                for i in chand:
                        ais = ais + cards[i]
                        print(ais)

        if ais > 21:
                print('my score was', ais)
                print('you win')
        else:
                print('my score was', ais)
                print('you lose')

when the computer wins it is meant to show the computers cards but it just shows a blank but when the player wins it shows the computers cards. i am quite new to python and might be missing something obvious but i don't know why this is going wrong

Upvotes: 0

Views: 42

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

while ais < value and ais < 21:

Is the issue you're facing. If ais == value this never runs, so the dealer never draws a card.

You need to use;

while ais <= value and ais < 21:

Furthermore, this ties in with an issue you have here;

if ais > 21:
    print('my score was', ais)
    print('you win')
else:
    print('my score was', ais)
    print('you lose')

You're only checking if the dealer has a hand of over 21. You do not check if there is a draw (This results in a loss).

You also do not set a value if the player does not draw a card, this means even if the user has a higher score then the dealer, the dealer will always win if the player does not draw.

welcome to black jack
type draw or sit to get started
your cards a, 8
what would you like to do: sit
you have sat
5
my score was 5
you lose

Upvotes: 1

Related Questions