[Tutor] Passing perimeters in dictionary values?

nathan virgil sdragon1984 at gmail.com
Tue Feb 24 20:03:09 CET 2009


I'm experimenting with OOP using the Critter Caretaker script from Python
Programming for the Absolute Beginner as my basis. I've noticed that a
dictionary/function combo is a great way to handle menus, and so I've
adapted the menu to read as:


selection = raw_input("Choice: ")
choices = {"0":quit, "1":crit.talk, "2":crit.eat, "3":crit.play}
choice = choices[selection]
choice()

so that I can call methods from a dictionary, instead of having an
excruciatingly long if structure. Unfortunately, the problem I'm running
into with this is that I can't pass any perimeters through the dictionary. I
can't figure out how, for example, I could have an option that calls
crit.eat(2) and another that calls crit.eat(4). The only thing I can think
of is going back to the if structure, but my instinct tells me that this is
a Bad Idea. What can I do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090224/b0e21837/attachment-0001.htm>


More information about the Tutor mailing list