What you need is an if
block instead of a try-except
block:
if not (usr_num_guess >= 1 and usr_num_guess < 100):
usr_num_guess = usr_input(True) # If the input number is out of range, the player gets a warning message + new input
The code in the except
part will only be executed if the line (usr_num_guess >= 1 and usr_num_guess < 100)
raises an exception.
On the use of exception, when you write
try:
...
except:
...
the code will catch every possible exception and execute th code in the except
block which is supposed to fix the situation. This is probably not what you want most of the time. For example, mis-spelling a variable (e.g. usr_num_guesss
) will raise a NameError
, and whatever user_input()
do will not fix the error -- it is an entirely different problem already than an out-of-range input. You should only catch and handle the exceptions that can be handled by your code, such as:
except ValueError:
...