1

I want to call a function named in the dictionary depending on the key called.

Example:

start_options = {'left': 'octopus', 'right': 'lion', 'small': 'pit', 'small door': 'pit'}

choice = raw_input("What do you want to do? ").lower()
        if choice in dictionary:
            print "found: " + choice, start_options[choice]
            print "You chose " + choice
            #code here will call function

def octopus():
    #do something
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    If you define `octopus` *before* `start_options`, you can make *the function itself* the value. – jonrsharpe Jun 02 '16 at 21:25
  • Duplicate of: http://stackoverflow.com/questions/3061/calling-a-function-of-a-module-from-a-string-with-the-functions-name-in-python – Tom Myddeltyn Jun 02 '16 at 21:38

1 Answers1

2

If you define the functions beforehand, you can then refer to them directly in your dictionary as opposed to string representations:

def yada():
    print("Yada!")

def blah():
    print("Blah!")

start_options = {'test1': yada, 'test2': blah}
start_options['test1']()

Running this code produces:

Yada!

Random Davis
  • 6,662
  • 4
  • 14
  • 24