fork download
  1. from functools import reduce
  2.  
  3. def str2num(s):
  4. if '.' in s: # 如果字符串中有小数点,则转换为浮点数
  5. return float(s)
  6. return int(s)
  7.  
  8. def calc(exp):
  9. ss = exp.split('+')
  10. ns = map(str2num, ss)
  11. return reduce(lambda acc, x: acc + x, ns)
  12.  
  13. def main():
  14. r = calc('100 + 200 + 345')
  15. print('100 + 200 + 345 =', r)
  16.  
  17. r = calc('99 + 88 + 7.6')
  18. print('99 + 88 + 7.6 =', r)
  19.  
  20. main()
  21.  
  22. # your code goes here
Success #stdin #stdout 0.11s 13988KB
stdin
Standard input is empty
stdout
100 + 200 + 345 = 645
99 + 88 + 7.6 = 194.6