Created
April 13, 2017 01:54
-
-
Save keyurgolani/238928260dcf1acf5c2c93314d969ea8 to your computer and use it in GitHub Desktop.
A simple python function that Prompts user on the standard console for confirmation.
The user can choose one of multiple meanings either meaning Yes or No.
Any other input will result in a bad input and the user will be prompted again.
The user will be allowed to enter bad inputs exactly equal to the value of 'retries' parameter.
Valid inputs fo…
This file contains 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 console_confirmation(question, retries=4, message='Bad answer! Please try again.'): | |
""" | |
Prompts user on the standard console for confirmation. | |
The user can choose one of multiple meanings either meaning Yes or No. | |
Any other input will result in a bad input and the user will be prompted again. | |
The user will be allowed to enter bad inputs exactly equal to the value of 'retries' parameter. | |
Valid inputs for Yes response are 'y', 'yes', 'sure', 'alright', 'right', 'all right', 'positive', 'certainly', 'for sure', 'indeed', 'agreed', 'absolutely', 'affirmative' | |
Valid inputs for No response are 'n', 'no', 'nope', 'na', 'nah', 'nada', 'negative', 'not really', 'not', 'no way', 'no ways' | |
""" | |
answer = raw_input(question + '\n') | |
if answer.lower() in ('y', 'yes', 'sure', 'alright', 'right', 'all right', 'positive', 'certainly', 'for sure', 'indeed', 'agreed', 'absolutely', 'affirmative'): | |
return True | |
if answer.lower() in ('n', 'no', 'nope', 'na', 'nah', 'nada', 'negative', 'not really', 'not', 'no way', 'no ways'): | |
return False | |
print message | |
if retries < 1: | |
raise ValueError('Multiple bad answers!') | |
else: | |
return console_confirmation(question, retries=retries - 1, message=message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment