Home > Software design >  How do I continue my try/catch in my while-loop
How do I continue my try/catch in my while-loop

Time:10-19

I'm making this blackjack game and I'm sure if you've played the game you know the rules. Basically I have 5 amount of chips and I'm letting the user type in their bets for the round. I have this try catch block thats supposed to not let the user type in anything below 0 and above the chip_amount. My exception for ValueError works fine and the while-loop continues if the user types "fff" or anything thats not a number. The program exits after if the user types in anything under 0 and above chip_amount, this is because the while-loop stops and I can't put continue in my if test, how can I solve this in a good way?

print("\n==== BLACKJACK GAME ====")

print(f'\nYou have currently have {chip_amount} chips available.')

while True:
    try:
        chips_input = int(input("How many chips do you want to bet? "))
        if chips_input < 1:
            raise Exception("Sorry, you have to enter a number bigger than 1.")
        if chips_input > chip_amount:
            raise Exception(f'Sorry, you have to enter a number less than {chip_amount}.')
    except ValueError:
        print("\nYou have to enter a number!")
        continue
    else:
        print(f'\nYou bet {chips_input} chips out of your total of {chip_amount} chips.')

        print(f'\nThe cards have been dealt. You have a {" and a ".join(player_hand)}, with a total value of {player_total}.')
        print(f'The dealers visible card is a {dealer_hand[0]}, with a value of {dealer_total_sliced}.')

CodePudding user response:

Raise ValueErrors so that your except block will catch these too:

if chips_input < 1:
    raise ValueError("Sorry, you have to enter a number bigger than 1.")
if chips_input > chip_amount:
    raise ValueError(f'Sorry, you have to enter a number less than {chip_amount}.')

CodePudding user response:

Instead of an infinite while True loop, you can loop while the reply is valid

chip_amount = 10

print("\n==== BLACKJACK GAME ====")
print(f'\nYou have currently have {chip_amount} chips available.')
chips_input = input("How many chips do you want to bet? ")

while not chips_input.isdigit() or not chip_amount >= int(chips_input) >= 0:
    print(f"False entry, enter a number between 0 and {chip_amount}")
    chips_input = input("How many chips do you want to bet? ")

print(f'\nYou bet {chips_input} chips out of your total of {chip_amount} chips.')

CodePudding user response:

I guess when you say "while loop stops" you mean program exits with an Exception. This is because you are only excepting ValueError exceptions, but you raise an Exception, so it isn't caught and the error terminates the program.

It is bad practice to use the general Exception anyway. You should use pick a concrete Exception from https://docs.python.org/3/library/exceptions.html

You could probably go with ValueError and catch them with the current except block you have

  • Related