I am trying to make calculator that can solve expressions with basic 4 operators, like 1+2*3-4/5, however it does not work and I do not know what is wrong. Please check my code.
When I run it, I am getting infinte number of errors in 8. line return ret(parts[0]) * ret(parts[2])
Here is code
def ret(s):
s = str(s)
if s.isdigit():
return float(s)
for c in ('*','/','+','-'):
parts = s.partition(c)
if c == '*':
return ret(parts[0]) * ret(parts[2])
elif c == '/':
return ret(parts[0]) / ret(parts[2])
elif c == '+':
return ret(parts[0]) + ret(parts[2])
elif c == '-':
return ret(parts[0]) - ret(parts[2])
print(ret('1+2'))
And the error traceback ends with:
File "C:\Python33\calc.py", line 8, in ret
return ret(parts[0]) * ret(parts[2])
File "C:\Python33\calc.py", line 2, in ret
s = str(s)
RuntimeError: maximum recursion depth exceeded while calling a Python object