Last active
January 21, 2020 18:16
-
-
Save nimaid/ef3753f8b634fbf966fadd2c0d2cd1d2 to your computer and use it in GitHub Desktop.
Easy-to-use Python3 function for simple user input.
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 ask_question(question='Please choose', options=['Y', 'N'], default=None): | |
opt_list = [] | |
for opt in options: | |
if opt not in opt_list: | |
opt_list.append(opt) | |
opt_list = [x.upper() for x in opt_list] | |
if len(opt_list) < 1: | |
raise Exception('No options provided!') | |
if default != None: | |
default = default.upper() | |
if default not in opt_list: | |
raise Exception('Default value is invalid!') | |
opt_string = '' | |
for i,opt in enumerate(opt_list): | |
if opt == default: | |
opt_string += '[' + opt + ']' | |
else: | |
opt_string += opt | |
if i+1 < len(opt_list): | |
opt_string += '/' | |
answer = None | |
error_string = 'That is not a valid option.' | |
while answer not in opt_list: | |
answer = input(question + ' ({}): '.format(opt_string)).strip().upper() | |
if answer in opt_list: | |
return answer | |
else: | |
if answer == '': | |
if (default == None) or (default not in opt_list): | |
print(error_string) | |
else: | |
return default | |
else: | |
print(error_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment