0

I'm making a blackjack game as a beginners project. I'm having trouble with exiting multiple loops. Instead of exiting the blackjack game and stopping the program that's running when the input is "Leave" the loop continues.

This is the part of my code that I'm having problems with.

def deal_cards():
    while deck != []:
       # code here that's irrelevant

        while player_sum <= 21:
            if player_sum == 21:
                print('BLACKJACK!')
                break
            move = input('What would you like to do: ') # this is the player input. When the input is 
            if move == 'Hit' or move == 'hit' or move == 'HIT' or move == 'HIt' or move == 'hIt':
                card = random.choice(deck)
                deck.remove(card)
                player_sum = player_sum + card
                print('You got a ' + str(card) + '. Your sum is ' + str(player_sum))
                if player_sum > 21:
                    print("YOU BUSTED")
                    break
            elif move == 'Stand' or 'stand' or 'STAND':
                print('The dealer has: ' + str(dealer_sum))
                while dealer_sum < 17:
                    card = random.choice(deck)
                    deck.remove(card)
                    dealer_sum = dealer_sum + card
                    print('The dealer has: ' + str(dealer_sum))
                if dealer_sum > player_sum and dealer_sum <= 21:
                    print('You lost')
                elif dealer_sum == player_sum:
                    print('Even. You get you money back')
                elif dealer_sum < player_sum:
                    print('YOU WIN!')
                elif dealer_sum > 21:
                    print('The dealer busted. YOU WIN!')
                break
            if move == 'Leave' or 'leave' or 'LEAVE': # this is where the 
# problem is. I want the entire program to end. I tried using break and
# return but I don't seem to get it wotking.

welcome_screen()
  • `return` will work. Your comparison is wrong. It should be `if move in {'Leave', 'leave', 'LEAVE'}:` or even better `if move.lower() == 'leave':`. – Matthias Aug 25 '19 at 17:54
  • If you want to end the entire program on some condition `exit()` method terminates the program. It also terminates, even if you are running things in parallel through the multiprocessing package. – ngShravil.py Aug 25 '19 at 18:03
  • @Barmar: I doubt the duplicate is OK. The OP clearly had a solution with `return` that would have worked if the condition would have been OK. – Matthias Aug 25 '19 at 18:03
  • Thanks guys. When I tried using `return` before it didn't work but it seems to be working now – Den Fula Ankungen Aug 25 '19 at 19:06

1 Answers1

0

try adding this line at the beginning of your program: class ExitLoop(Exception): pass. this is a custom error. now wrap all the loops in a try statement and make an except like this: except ExitLoop: pass this way when the ExitLoop is raised you will be able to jump to the except and skip everything else

PMM
  • 366
  • 1
  • 10