Would suggest you to use
User-Defined Exception
in Python using
try-except
Refer:
How to Define Custom Exceptions in Python? (With Examples)[
^]
Example from there:
class Error(Exception)
pass
class ValueTooSmallError(Error)
pass
class ValueTooLargeError(Error)
pass
number = 10
while True:
try:
i_num = int(input("Enter a number: "))
if i_num < number:
raise ValueTooSmallError
elif i_num > number:
raise ValueTooLargeError
break
except ValueTooSmallError:
print("This value is too small, try again!")
print()
except ValueTooLargeError:
print("This value is too large, try again!")
print()
print("Congratulations! You guessed it correctly.")