Created
June 13, 2013 09:01
-
-
Save rob-b/5772283 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
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| def easy_input(question, answer=None, default=None): | |
| """Ask a question, return an answer. | |
| <question> is a string that is presented to the user. | |
| <answer> is a list of strings presented as a choice. User may type only first letters | |
| <default> is the presumed answer if the user just hits <Enter>. | |
| """ | |
| if answer is None: | |
| answer = ['yes', 'no'] | |
| else: | |
| answer = [i.lower() for i in answer] | |
| # if <default> is None or <default> is not an expected answers | |
| # <default> will be the first of the expected answers | |
| if default is None or default not in answer: | |
| default = answer[0] | |
| prompt = '/'.join([ | |
| "\x1b[1;1m{0}\x1b[1;m".format(i.capitalize()) if i == default else i | |
| for i in answer | |
| ]) | |
| while True: | |
| choice = raw_input("{0} [{1}]: ".format(question, prompt)).lower() | |
| if default is not None and choice == '': | |
| return default | |
| if choice in answer: | |
| return choice | |
| valid_answer = {i[:len(choice)]: i for i in answer} | |
| if len(valid_answer) < len(answer): | |
| print(" -- Ambiguous, please use a more detailed answer.") | |
| elif choice in valid_answer: | |
| return valid_answer[choice] | |
| else: | |
| print(" -- Please answer only with {0} or {1}.".format(", ".join(answer[:-1]), answer[-1])) | |
| if __name__ == '__main__': | |
| u = easy_input("vous habitez chez vos parents ?") | |
| print("\nanswer was : {0}\n".format(u)) | |
| u = easy_input("ajouter au gloubiboulga ?", ['carotte', 'rutabaga', 'pomepeche']) | |
| print("\nanswer was : {0}\n".format(u)) | |
| u = easy_input("alors, on dance ?", ['non', 'nui'], 'oui') | |
| print("\nanswer was : {0}\n".format(u)) | |
| u = easy_input("question piège ?", ['continue', 'testicle', 'test', ]) | |
| print("\nanswer was : {0}\n".format(u)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment