Last active
July 16, 2019 07:20
-
-
Save baileywickham/348be4402558759d5af68f9a45c0fe0a to your computer and use it in GitHub Desktop.
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 get_data(func, message): | |
while True: | |
try: | |
tmp = input(message) | |
tmp = func(tmp) | |
except: | |
print(f"Input not of type: {func.__name__}") | |
else: | |
return tmp | |
# You may also define generic types with a function | |
def int_less_than_ten(i): | |
i = int(i) | |
if i > 10: | |
raise ValueError | |
return i | |
print(get_data(str, 'What is your favorite color?\n')) | |
print(get_data(int_less_than_ten, 'How old are you?\n')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment