Jacob S. wrote:
while 1:
play = raw_input("What is your choice? ")
if play in options.keys(): ## This makes sure that the user input is one of our options

'if play in options' is preferable. options.keys() returns a list of keys that will be searched sequentially. options is a dict which supports fast direct lookup. So when you say 'if play in options.keys()' not only do you type more but you generate an intermediate list and use a slower form of search!


options[play]() ## Call the function that is the value of the key that is the choice the user made
else: ## Say that the user input is not one of your choices...
print "\nYou need to pick 1, 2 or 3 or hit enter to see choices\n"

But even better is to make the error message a default option:

def bad_choice():
  print "\nYou need to pick 1, 2 or 3 or hit enter to see choices\n"

Then the lookup becomes just
  options.get(play, bad_choice)()

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to