Most Pythonic way to do input validation

Viewed 17135

What is the most "correct", Pythonic way to do user input validation in Python?

I've been using the following:

while True:
    stuff = input("Please enter foo: ")
    try:
        some_test(stuff)
        print("Thanks.")
        break
    except SomeException:
        print("Invalid input.")

Which is nice and readable, I suppose, but I can't help wondering if there isn't some built-in function or something I should be using instead.

3 Answers
Related