首页 > 解决方案 > 如何在python上计算字符串方程,包括+、-、*、/不带eval

问题描述

'1+2*3*4+5-1+6/2' 我的意思是,当我每次搜索时给出一个字符串会给我结果 32 而不使用 eval 时,它给了我 eval 的答案。

每次我尝试输入乘法时,即使我先将它放在不同的 for 上,它也不起作用,并且 Len 是小写字母,但它不允许我上传问题,所以我用大写字母写了它:

def compute(string):
    total = 0

    for index in range(Len(string)):
     if string[index] == '+':
            total += int(string[index+1])
            print total
        if string[index] == '-':
            total -=int(string[index+1])
            print total

标签: python

解决方案


推荐阅读