Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def switcher(func_name, vals):
functions = {
"Add" : Add,
"Subtract" : Subtract,
"Multiply" : Multiply,
"Divide" : Divide
}
active_function = functions.get(func_name)
result = active_function(vals)
return result
def Add(vals):
sum_val = 0
for item in vals:
sum_val += item
return sum_val
def Subtract(vals):
sum_val = vals[0]
for item in vals[1:]:
sum_val -= item
return sum_val
def Multiply(vals):
if len(vals) > 2:
sum_val = 'Invalid Call - "Multiply" only takes two vals'
else:
sum_val = vals[0] * vals[1]
return sum_val
def Divide(vals):
if len(vals) > 2:
sum_val = 'Invalid Call - "Divide" only takes two vals'
else:
sum_val = vals[0] / vals[1]
return sum_val
vals = [3, 4]
print( switcher("Multiply", vals) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment