I fixed your code, it is very simple:
def app():
while True:
name = input('What is your name: ')
age = input('How old are you?: ')
try:
if float(age) < 18:
print(f'{name}, you are a child')
else:
print(f'{name}, you are an adult')
break
except ValueError:
print('Please enter your age as a number')
Number can be either integer or fraction, use float
so we can handle numbers with decimal points.
A number can only be less than 18 or not less than 18, so use else
instead of a second conditional checking (elif
).
Trying to cast a non-numeric string to a float ('two'
) will raise ValueError
, we can use try-except block to catch the error and validate the input.
Finally we can use a while
loop to keep asking for input and only stop we valid input is entered.