I am executing the below code to catch input that is not a number or the number not within the range. i could catch the assertionerror but not the value error. can someone help me understand why i am not catching the valueerror?
What i am trying to do:
i am getting the input from the user to see whether x is a number and it falls within the range specified that is min and max. if the input fails the validation, i should request the user to input the value again until the validation is passed
so:
when x = <<string>>
, i should get "Enter a valid int"
message.
When x
not within range, i should get "Enter a number between -10 and 10"
.
def readint(x,min,max):
try:
#x=int(input("Enter number betwen min and max: "))
assert (x<=max and x>=-min)
except ValueError:
print("Enter a valid int")
except AssertionError:
print("Enter a number between -10 and 10")
except NameError:
print("Enter a valid int")
while True:
readint(x=int(input("Enter number betwen min and max: ")),min=-10,max=10)