accept input only if correct values where entered previously code example
Example 1: python check if input() gives error
try:
age = int(input("Please enter your age: "))
except ValueError:
print("Sorry, I didn't understand that.")
else:
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
Example 2: python double check if wants to execute funtion
while True:
try:
age = int(input("Please enter your age: "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
if age < 0:
print("Sorry, your response must not be negative.")
continue
else:
break
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")