Skip to content

Instantly share code, notes, and snippets.

@triffid
Created June 18, 2012 13:16
Show Gist options
  • Save triffid/2948317 to your computer and use it in GitHub Desktop.
Save triffid/2948317 to your computer and use it in GitHub Desktop.
python menu
import collections
def menu(items):
while 1:
for item in range(0, len(items)):
print "%d - %s" % (item + 1, items[item])
selection = raw_input("> ")
try:
selection = int(selection) + 1
if (selection >= 0) and (selection < len(items)):
return selection
except ValueError:
for item in range(0, len(items)):
if items[item].lower() == selection.lower():
return item
print "Invalid selection"
def menu2(items):
while 1:
for item in range(0, len(items)):
print "%d - %s" % (item + 1, items.keys()[item])
selection = raw_input("> ")
try:
selection = int(selection) - 1
if (selection >= 0) and (selection < len(items)):
return items.values()[selection]()
except ValueError:
for item in range(0, len(items)):
if items.keys()[item].lower() == selection.lower():
return items.values()[item]()
print "Invalid selection"
def ItemOne():
print "one called"
def ItemTwo():
print "two called"
menuItems = collections.OrderedDict()
menuItems["One"] = ItemOne
menuItems["Two"] = ItemTwo
menuItems["Game"] = mobSpawn
menuItems["Math"] = mathMenu
menu2(menuItems)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment