python 中 eval 对字符串数值进行计算

 

001、

>>> eval("10 + 100")        ##  对字符串数值进行计算
110
>>> eval("10 - 100")
-90
>>> eval("10 * 100")
1000
>>> eval("10 / 100")
0.1
>>> eval(10 / 100)          ## 参数需要是字符串
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: eval() arg 1 must be a string, bytes or code object

。 

 

posted @ 2024-12-10 10:40  小鲨鱼2018  阅读(48)  评论(0)    收藏  举报