In Python3, the input()
function always returns a string, even if the string entirely consists of digits, it still returns it as a string, e.g: "123"
.
So, trying to catch an error when converting it to a string will never work as it is always already a string!
The correct way to do this would be to use .isdigit()
to check if it is an integer.
while True:
member = input('Are you a child or adult member? ');
if member.isdigit():
print('Please input letters')
else:
break
However, if you want to validate properly, you should not allow the user to enter any string, you should restrict them to "child"
or "adult"
:
while True:
member = input('Are you a child or adult member? ');
if member not in ('child', 'adult'):
print('Please enter a valid entry.')
else:
break