Ask a yes/no question via raw_input() and return their answer. "question" is a string that is presented to the user. The "answer" return value is True for "yes" or False for "no".
(question='')
| 3 | |
| 4 | |
| 5 | def query_yes_no(question='') -> bool: #https://code.activestate.com/recipes/577058/ |
| 6 | """Ask a yes/no question via raw_input() and return their answer. |
| 7 | "question" is a string that is presented to the user. |
| 8 | The "answer" return value is True for "yes" or False for "no". |
| 9 | """ |
| 10 | # Always return false if not in an interactive shell |
| 11 | if not sys.stdin.isatty(): |
| 12 | return False |
| 13 | |
| 14 | valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False} |
| 15 | |
| 16 | while True: |
| 17 | choice = '' |
| 18 | try: |
| 19 | choice = inputimeout(prompt=question, timeout=5) |
| 20 | except TimeoutOccurred: |
| 21 | sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n')") |
| 22 | sys.stdout.flush() |
| 23 | |
| 24 | if choice in valid: |
| 25 | return valid[choice] |
no test coverage detected