python - 如何在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
解决方案
推荐阅读
- c# - Null-coalescing out 参数给出意外警告
- python-3.x - 导入请求似乎只能在 shell 中工作
- javascript - JavaScript 将带有数字字符串的键转换为数字......但 Object.keys() 没有
- .htaccess - 用查询字符串问题重写 url
- php - PHP数据库连接到本地主机错误
- excel - Google表格 - 从范围A返回N个第一个值的公式,其中MAX(范围B)
- forms - 我如何为 bonitasoft 5.10 制作“后退按钮”
- r - 如何为 R 中的对象创建一个伪造的二进制文件以获取其 md5 哈希?
- c# - C# 的字节码格式是什么?
- c++ - 删除重复的变量分配行