[Tutor] using a function as a dictionary value?

justinstraube at charter.net justinstraube at charter.net
Mon Dec 13 10:53:51 CET 2004


When I run this, regardless of which option I select all three functions are 
called. Is there a way I can do this where only the option selcted is called? 
Im using 2.3.3 if that matters any. Thanks.

#####
def spam():
    print 'Hello world'

def breakfast():
    print 'Spam, Spam, Chips, and Spam'

def bridgekeeper():
    print 'Ask me your questions, Bridgekeeper. I am not afraid.'

select = raw_input('Chose an option [1|2|3]: ')

options = ['1', '2', '3']
command = {'1': spam(),
            '2': breakfast(),
            '3': bridgekeeper()
            }

if select in options:
    command[select]

else:
    print 'Selection not recognized.'
#####

>>>
chose an option [1|2|3]: 3
Hello world
Spam, Spam, Chips, and Spam
Ask me your questions, Bridgekeeper. I am not afraid.
>>>



regards,

Justin

---



More information about the Tutor mailing list