[Tutor] Passing perimeters in dictionary values?

nathan virgil sdragon1984 at gmail.com
Wed Feb 25 01:25:02 CET 2009


On Tue, Feb 24, 2009 at 6:50 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
>
> Or you can build the param value into the dictionary:
>
> selection = raw_input("Choice: ")
> choices = {
> "0":(quit, None),
> "1":(crit.talk, "Hi there"),
> "2":(crit.eat, "Nuts"),
> "3": (crit.play, "Soccer")
> "4": (crit.play, "Baseball")}      # same func different param
> choice = choices[selection]
> choice[0](choice[1])
>

This actually suits my needs wonderfully. Thanks!


>
> 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 question is where are the 2 and 4 coming from?
> You could read them from the user or a file in option 1 above
> or store them with the function under a different menu in option 2.
> Or of course you could just hard code them at call time...


>
> It depends how you want to use it...
>

The "critter" has attributes for hunger and boredom; if you do anything but
eat (for hunger) or play (for boredom), the attribute value goes up, and
affects the critter's mood. If you access the right method, the
corresponding attribute will go down by a certain value. As it is right now,
it only uses the default value, but I was thinking of having different
options for the method. Maybe the critter finds cabbage more tasty/filling
then lettuce, or maybe it prefers playing baseball over playing soccer. This
could be represented by having cabbage represented by crit.eat(4) and
lettuce by crit.eat(2). In this case, getting the perimeters directly from
the user would be a Very Bad Idea, but I'm sure there's some scenarios where
it wouldn't be.


>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090224/29e126a7/attachment.htm>


More information about the Tutor mailing list