Skip to content

Instantly share code, notes, and snippets.

@rurtubia
Last active August 29, 2015 14:22
Show Gist options
  • Save rurtubia/e7f264a86c2159e3dd02 to your computer and use it in GitHub Desktop.
Save rurtubia/e7f264a86c2159e3dd02 to your computer and use it in GitHub Desktop.
try except in python validates if the input is an int validates if the input is 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
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'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment