Search code examples
pythonpython-2.7eval

strange issue while using compile and eval in python


I am trying to parse and eval a formula using python eval and compile. For one case it is working, for other case it is not. see below: This is working fine

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

but this gives error

>>> 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

Please help. I am unable to understand. I am using Python 2.7.5


Solution

  • The leading zero makes python treat the value as an octal number. 08 is invalid, as octal numbers must only contain the digits 0-7. You should remove the leading zero.