python计算器总结,python中的计算器

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

解决方案

You partition the input string regardless, never checking if the operator is even there. .partition() returns empty strings if the partition character is not present in the input:

>>> '1+1'.partition('*')

('1+1', '', '')

So you'll call s.partition('*') but never check if there is any such operator present, resulting in unconditional calls to ret(). You'll always call ret(parts[0]) * ret(parts[2]) regardless of wether * is present in s or not.

The solution is to either test for the operator first or to check the return value of .partition(). The latter is probably easiest:

for c in ('+','-','*','/'):

parts = s.partition(c)

if parts[1] == '*':

return ret(parts[0]) * ret(parts[2])

elif parts[1] == '/':

return ret(parts[0]) / ret(parts[2])

elif parts[1] == '+':

return ret(parts[0]) + ret(parts[2])

elif parts[1] == '-':

return ret(parts[0]) - ret(parts[2])

Note that I reversed the operator order; yes, multiplication and division need to be applied before addition and subtraction, but you are working in reverse here; splitting up the expression into smaller parts, and the operations are then applied when the sub-expression has been processed.

You could use assignment unpacking to assign the 3 return values of .partition() to easier names:

for c in ('+','-','*','/'):

left, operator, right = s.partition(c)

if operator == '*':

return ret(left) * ret(right)

elif operator == '/':

return ret(left) / ret(right)

elif operator == '+':

return ret(left) + ret(right)

elif operator == '-':

return ret(left) - ret(right)

Next you can simplify all this by using the operator module, which has functions that perform the same operations as your arithmetic operations. A map should do:

import operator

ops = {'*': operator.mul, '/': operator.div, '+': operator.add, '-': operator.sub}

for c in ('+','-','*','/'):

left, operator, right = s.partition(c)

if operator in ops:

return ops[operator](ret(left), ret(right))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值