Python: Invalid Token

你说的曾经没有我的故事 提交于 2019-11-26 05:56:46

问题


Some of you may recognize this as Project Euler\'s problem number 11. The one with the grid.

I\'m trying to replicate the grid in a large multidimensional array, But it\'s giving me a syntax error and i\'m not sure why

grid = [ 
[ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 ],
[ 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 ],
[ 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65 ],
...

And I get this error:

  File \"D:\\development\\Python\\ProjectEuler\\p11.py\", line 3
    [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91 , 08 ],
       ^ SyntaxError: invalid token

Why is it throwing an error before the comma?


回答1:


I think when you start a literal number with a 0, it interprets it as an octal number and you can't have an '8' in an octal number.




回答2:


Note that the "^" symbol in the error points exactly to the erroneous column. Together with the line number it points exactly on the digit 8. This can help lead you to what Jeremy suggested.




回答3:


Just remove leading zeros.

First zero makes number octal.



来源:https://stackoverflow.com/questions/336181/python-invalid-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!