I am struggling with this recursion problem. I want to create a calculator to basic operations in python with recursion.
ops = {"+": (lambda x,y: x+y), "-": (lambda x,y: x-y), "*": (lambda x,y: x*y)}
def calculator(expr):
for i in expr:
if type(i) != tuple:
return (ops[expr[1]] (expr[0],expr[2]))
else:
return calculator((i))
for calculator(((1, '+', 2), '*', 3))
I expected 9
but i get (1, '+', 2, 1, '+', 2, 1, '+', 2)
Please can you help?