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}.')
Advertisement
Answer
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}.')