Search code examples
pythonfunctionvariablesblackjack

Blackjack game not updating hands each time everyone draws


I'm trying to make a simple text-based Python game around blackjack. The game knows the variable 'hand' and 'd_hand' which are your hand and the dealer's hand, but won't update it after each new card is drawn. hand and d_hand are assigned to a random integer between 1 and 11 plus your current hand before it added the random number, which in theory should mean that your hand updates itself every time a new card is drawn. Here's the code:

def draw(hand, d_hand):
x = randint(1, 11)
card = x
hand = x + hand
print("You drew...")
print(card)
y = randint(1, 11)
d_card = y
d_hand = y + d_hand
print("Dealer drew...")
print(d_card)
print("Your hand: ")
print(hand)
print("Dealer's hand: ")
print(d_hand)
ask()

And here's the output of everything: (Note: I only am showing one function here, the game is obviously more than just this one function I'm showing.)

Press enter to begin: 
You drew...
1
Dealer drew...
5
Your hand: 
1
Dealer's hand: 
5
Hit or stay? (h/s): h
You drew...
10
Dealer drew...
8
Your hand: 
10
Dealer's hand: 
8
Hit or stay? (h/s): '''

I'm not really sure what the issue is here...

By the way, I'm new to this site so I can't like any comments, so thank you to everyone who answered!


Solution

  • If hand and d_hand are lists (or mutable objets),

    you may want to update the object itself by replacing hand = x + hand with hand.append(x).

    Otherwise, your code will just create a new local list hand that will be lost when the function ends.