How to ask the user for input until they give a valid response in Python. Here's one clean way:
- use a while True loop
- use input() to get the user input
- use a try-except block to catch invalid inputs
- use an else block to break if the input is valid
while True:
try:
age = int(input("Please enter your age: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
break