相关疑难解决方法(0)

评估字符串中的数学表达式

stringExp = "2^4"
intVal = int(stringExp)      # Expected value: 16
Run Code Online (Sandbox Code Playgroud)

这将返回以下错误:

Traceback (most recent call last):  
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int()
with base 10: '2^4'
Run Code Online (Sandbox Code Playgroud)

我知道eval可以解决这个问题,但是不是有更好的 - 更重要的 - 更安全的方法来评估存储在字符串中的数学表达式吗?

python math

99
推荐指数
10
解决办法
10万
查看次数

标签 统计

math ×1

python ×1