1

So I was doing a blackjack program. Everything was working until I got this:

Your cards:  [2, 4] 
Total:  6
Chose your next move: stand

Dealer's cards:  [5]
Your cards:  [2, 4, 10] 
Total:  16
Chose your next move: stand


//////////////////////////
Dealer's cards:  [5]
Your cards:  [2, 4, 10, 10] 
Total:  26 
//////////////////////////

The loop is supposed to break when move == stand I think it's the break function, but there is a high chance I messed something else up. Here's the bit of code I think is messing up:

while player_cards_total < 21:
  player_cards_total = sum(player_cards)
  dealer_cards_total = sum(dealer_cards)
  if player_cards_total > 20:
    print('\n\n//////////////////////////\nDealer\'s cards: ', dealer_cards)
    print('Your cards: ', player_cards,'\nTotal: ', player_cards_total, '\n//////////////////////////')
    print('\nBUST\n')
    break
  move = get_move()
  
  if move == 'hit':
    player_cards.append(get_card())
  else:
    break

The while loop is an individual loop, and not a inner loop

Here's the whole code

import time



Ace = 11
Jack = 10
Queen = 10
King = 10


cards = [Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King]



Ace_21 = False
player_bal = 0
dealer_bal = 0
player_cards = []
dealer_cards = []
player_cards_total = 0
dealer_cards_total = 0
card = ''
move = ''
moves = 0

def get_card():
  return(int(cards[random.randrange(1, 13)]))


dealer_cards =  [get_card(),]
player_cards = [get_card(), get_card()]
player_cards_total = sum(player_cards)


def get_move():
  
  if moves == 0:
    print('\nDealer\'s cards: ', dealer_cards)
    print('Your cards: ', player_cards,'\nTotal: ', player_cards_total)
  move = input('Chose your next move: ')
  if move == 'h' or 'Hit':
    move = 'hit'
  elif move == 's' or 'Stand':
    move = 'stand'
  return(move)



while player_cards_total < 21:
  player_cards_total = sum(player_cards)
  dealer_cards_total = sum(dealer_cards)
  if player_cards_total > 20:
    print('\n\n//////////////////////////\nDealer\'s cards: ', dealer_cards)
    print('Your cards: ', player_cards,'\nTotal: ', player_cards_total, '\n//////////////////////////')
    print('\nBUST\n')
    break
  move = get_move()
  
  if move == 'hit':
    player_cards.append(get_card())
  else:
    break


if player_cards_total > 21:
   print('You lose!!!')
elif player_cards_total == 21:
  print('Great job, you win')
else:
  print('DEALER\'S TURN')
  while dealer_cards_total < 20:
    dealer_cards_total = sum(dealer_cards)
Vyvix
  • 25
  • 3
  • 1
    You never run the break command as your while loop doesn't run when the if statement is true – moo Feb 16 '22 at 18:03
  • `get_move` always returns `"hit"` no matter what the actual `input()` value is. To understand why, see the linked duplicate. For future reference, please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to find problems like this yourself first. You should at least have suspected that, given that the `break` is supposed to break the loop, and that code should be reached whenever `if move == 'hit':` fails, that **maybe `if move == 'hit':` is succeeding**. Which is to say, maybe `move` **is** equal to `'hit'`. Which means, maybe `get_move` does something wrong. – Karl Knechtel Feb 16 '22 at 18:15

1 Answers1

1

get_move always returns 'hit', so the break can never run. This is caused by a logic error.

You need to change the following lines:

if move == 'h' or 'Hit':
#and
elif move == 's' or 'Stand':

Now to the right of "or" is a non-empty string so these if's will always be True.

Instead you need:

if move == 'h' or move == 'Hit':
#and
elif move == 's' or move == 'Stand':

This will actually test of move is equal to either string separately as you intended. Furthermore, you could also use this convention if you would like:

if move in ['h', 'Hit']:
#and
elif move in ['s', 'Stand']:
Eli Harold
  • 2,280
  • 1
  • 3
  • 22