gpt4 book ai didi

python - 在 python 中使用 compile 和 eval 时出现奇怪的问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:05:00 25 4
gpt4 key购买 nike

我正在尝试使用 python eval 和 compile 来解析和评估公式。对于一种情况,它正在工作,对于其他情况,它不是。见下文:这工作正常

>>> formula = '(00*3600)+(03*60)+10'
>>> eval(compile(formula, '<string>', 'eval'))
190

但这会出错

>>> formula = '(00*3600)+(08*60)+53'
>>> eval(compile(formula, '<string>', 'eval'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
(00*3600)+(08*60)+53
^
SyntaxError: invalid token

请帮忙。我无法理解。我正在使用 Python 2.7.5

最佳答案

前导零使 python 将该值视为八进制数。 08 无效,因为八进制数只能包含数字 0-7。您应该删除前导零。

关于python - 在 python 中使用 compile 和 eval 时出现奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55299585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com