Skip to content

TypeScript code snippet – python How to use while loop to handle invalid inputs?

while True:
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")

    if age < 0:
        print("Sorry, your response must not be negative.")
        #age was successfully parsed, and we're happy with its value.
        #we're ready to exit the loop.
if age >= 18: 
    print("You are able to vote in the United States!")
    print("You are not able to vote in the United States.")
See also  CSS code snippet - How to remove link blue color from a tag ?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.