iamtesla
iamtesla

Reputation: 423

python blackjack game, loop generating new values each time

def deck():
    cards = range(1, 12)
    return choice(cards)

def player():
    card1 = deck()
    card2 = deck()
    hand = card1 + card2
    print card1, card2
    while hand < 21:
        choice = raw_input("Would you like to hit or stand?: ")
        print choice
        if choice == "hit":
            hand2 = hand + deck()
            print hand2     
        elif choice == "stand": 
                return hand 

Hello all,

I am trying to make a simple blackjack game in python. I have gotten this far and I seem to be stuck. When I try and play it asks me whether or I want to hit or stand which is good; however, my problem is that it seems to be generating new card values each time. That means if I was to hit and than stand it would return the original value rather than the new value of the three cards.

As you can tell I'm new to programming so any help is appreciated, I'd like to use as much as my own code as possible.

Upvotes: 1

Views: 630

Answers (2)

John Kugelman
John Kugelman

Reputation: 361927

if choice == "hit":
      hand2 = hand + deck()
      print hand2     

The "hit" block modifies hand2 rather than hand.

if choice == "hit":
      hand += deck()
      print hand

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213351

If I understand your question correctly, then probably hand2 = hand + deck() inside your if is creating the problem.. You are assigning the sum to the new value, which is not even used anywhere.. You need to update the same hand. So replace the above statement with: -

hand = hand + deck()

Upvotes: 2

Related Questions