Skip to content

Instantly share code, notes, and snippets.

@keyurgolani
Created April 13, 2017 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyurgolani/238928260dcf1acf5c2c93314d969ea8 to your computer and use it in GitHub Desktop.
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…
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