Last active
February 16, 2017 07:12
-
-
Save quadrupleslap/960c33b51de7346a00cb8a86c3a080cf to your computer and use it in GitHub Desktop.
Simple function to choose from a range of options.
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
alphabet = "abcdefghijklmnopqrstuvwxyz" | |
def choose(question, options): | |
assert 1 < len(options) <= len(alphabet) | |
while True: | |
print(question) | |
for i, opt in enumerate(options): | |
print("{}) {}".format(alphabet[i], opt)) | |
inp = input("(a-{})> ".format(alphabet[i])) | |
if len(inp) == 1: | |
index = alphabet.find(inp) | |
if -1 < index < len(options): | |
print("") | |
return index | |
print("Invalid input.\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment