Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raganmd/49b460afeadf1e93e3894b15dfbcbbda to your computer and use it in GitHub Desktop.
Save raganmd/49b460afeadf1e93e3894b15dfbcbbda to your computer and use it in GitHub Desktop.
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