Skip to content

Instantly share code, notes, and snippets.

@hexsprite
Created January 25, 2014 23:44
Show Gist options
  • Select an option

  • Save hexsprite/8625585 to your computer and use it in GitHub Desktop.

Select an option

Save hexsprite/8625585 to your computer and use it in GitHub Desktop.
def choose_from(choices, prompt="Which do you choose?"):
while True:
for index, choice in enumerate(choices):
print "%2d. %s" % (index, choice)
choice = raw_input("\n%s " % prompt)
try:
choice = int(choice)
if choice < 0:
raise ValueError
return choices[choice]
except (ValueError, IndexError):
print "Invalid choice"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment