Created
September 15, 2013 13:54
-
-
Save IQAndreas/6570952 to your computer and use it in GitHub Desktop.
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
# Taken from http://code.activestate.com/recipes/577058-query-yesno/ | |
# with some personal modifications | |
def yes_no(question, default=True): | |
"""Ask a yes/no question via raw_input() and return their answer. | |
"question" is a string that is presented to the user. | |
"default" is the presumed answer if the user just hits <Enter>. | |
It must be "yes" (the default), "no" or None (meaning | |
an answer is required of the user). | |
The "answer" return value is one of "yes" or "no". | |
""" | |
valid = {"yes":True, "y":True, "ye":True, | |
"no":False, "n":False } | |
if default == None: | |
prompt = " [y/n] " | |
elif default == True: | |
prompt = " [Y/n] " | |
elif default == False: | |
prompt = " [y/N] " | |
else: | |
raise ValueError("invalid default answer: '%s'" % default) | |
while 1: | |
sys.stdout.write(question + prompt) | |
choice = input().lower() | |
if default is not None and choice == '': | |
return default | |
elif choice in valid.keys(): | |
return valid[choice] | |
else: | |
sys.stdout.write("Please respond with 'yes' or 'no' "\ | |
"(or 'y' or 'n').\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment