Last active
August 29, 2015 14:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Builtin Exceptions: | |
https://docs.python.org/2/library/exceptions.html#bltin-exceptions | |
Errors and Exceptions | |
https://docs.python.org/2/tutorial/errors.html#exceptions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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