Skip to content

Instantly share code, notes, and snippets.

@baileywickham
Last active July 16, 2019 07:20
Show Gist options
  • Save baileywickham/348be4402558759d5af68f9a45c0fe0a to your computer and use it in GitHub Desktop.
Save baileywickham/348be4402558759d5af68f9a45c0fe0a to your computer and use it in GitHub Desktop.
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