15

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?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
The.Anti.9
  • 43,474
  • 48
  • 123
  • 161

3 Answers3

40

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.

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
3

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.

yairchu
  • 23,680
  • 7
  • 69
  • 109
  • Although you provided a nice tip, this doesn't answer the question. So I think this should be a comment and not an answer. -1. – Bonifacio2 Sep 22 '14 at 18:49
  • 1
    @Bonifacio2: I think not only is this an answer, but also a good one, even if it doesn't fully answer the OP's question. This answer helps the OP parse the syntax error that got. Originally (prior to edit) he asked "Why is it throwing an error after the comma?" when in fact the error is before the comma, and my answer can help him figure out where the error was. "give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime" – yairchu Sep 28 '14 at 13:51
1

Just remove leading zeros.

First zero makes number octal.

maxp
  • 5,454
  • 7
  • 28
  • 30