-2

I am fairly new to python and I am not sure how to make a question involving an integer asked again when an invalid character is entered or it is left blank. Could someone please help me.

I have only tried what I know and nothing has really worked.

# Asks for age
age = int(input("Please enter your age: "))
# Prevents age from being left blank
# I need help with this part

1 Answers1

0
while True:
    try:
        age = int(input("Please enter your age: "))
    except ValueError:
        continue
    break

This runs in a loop until you get a valid value

Ron Serruya
  • 3,988
  • 1
  • 16
  • 26