rurtubia
6/1/2015 - 11:40 AM

try except in python validates if the input is an int validates if the input is either 1 or 2

try except in python validates if the input is an int validates if the input is either 1 or 2

def validateInput():
    try:
        print("Write either 1 or 2")
        one_or_two = input()
        one_or_two = int(one_or_two)
        if not ((one_or_two == 1) or (one_or_two == 2)):
            print("Write only either '1' or '2'")
    except ValueError:
        print("Write only either '1' or '2'")
Builtin Exceptions:

https://docs.python.org/2/library/exceptions.html#bltin-exceptions

Errors and Exceptions

https://docs.python.org/2/tutorial/errors.html#exceptions