Python input validation for both integer and string
10,167
as jamylak suggested change the if condition to :
if a == 'y' or 1 <= int(a) <= 10:
program:
def checkingInput():
while True:
try:
a = input('enter')
if a == 'y' or 1 <= int(a) <= 10:
return a
else:
print('Invalid input!')
except ValueError:
print('Value error! Please try again!')
Author by
Admin
Updated on June 07, 2022Comments
-
Admin almost 2 years
I'm new in programming and I have an issue when doing the input validation. My program requires to input number from 1 to 10 or the letter
y
but it seems that I cannot do an error handler for this.def checkingInput(): while True: try: a = input() if 10 >= a >= 1 or a == 'y': return value else: print('Invalid input!') except NameError: print('Name error!Please try again!') except SyntaxError: print('Syntax Error!Please try again!')