Please send questions to
st10@humboldt.edu .
#---------------------------------------------------------
# Module name: lect10_5.py
# part 4: using a dict to implement a multiway
# branch, in which each key's value is a function
# with the help of lambda expressions
# By: Sharon M. Tuttle
# last modified: 10-26-05
#---------------------------------------------------------
action_dict = {'already': lambda val: val + 2,
'got': lambda item: item * 3,
'one': lambda num: num ** 3
}
for ct in range(0, 5):
choice = raw_input('enter word: ')
val = raw_input('enter number: ')
val = int(val)
# print result of calling function for that choice
# with that val as its argument
# (if choice isn't in dict, call a function whose
# value is its argument)
print (action_dict.get(choice, lambda it: it)) (val)
# above is equivalent to:
chosen_funct = action_dict.get(choice, lambda it: it)
print chosen_funct(val)