2

I understand that a weird error I had was coming from the fact that some input numbers were interpreted as octal. But how comes, in the following line, that for example a "9" does not generate an error, but a "8" does?

    MY_LIST = [152,187,267,362,935,040,097,262,292,333,135,334,337,144,288,317,3
43,172,032,160,289,186,916,039,274,069,018,911,081,286,356]
                                         ^
SyntaxError: invalid token
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Antonio
  • 19,451
  • 13
  • 99
  • 197

1 Answers1

3

069 is not a valid octal number, and neither are several others, including 018.

Python is pointing you to 097 however, the first number starting with 0 that is not a valid octal number. Your console wrapped the error message and you interpreted the error message as pointing at the 018 value, but it is really the line above that that contains the error.

Compare the wrapped version:

    MY_LIST = [152,187,267,362,935,040,097,262,292,333,135,334,337,144,288,317,3
43,172,032,160,289,186,916,039,274,069,018,911,081,286,356]
                                         ^

with the un-wrapped version:

MY_LIST = [152,187,267,362,935,040,097,262,292,333,135,334,337,144,288,317,343,172,032,160,289,186,916,039,274,069,018,911,081,286,356]
                                     ^

It is your console that did the wrapping, not Python.

The preceding number 040 is valid octal and all numbers before that all start with a digit other than 0 and are not octal numbers.

Note that in my experience, the Windows console isn't directly resizable; you'd have to alter the console settings to increase the column count if you wanted to test against a larger or smaller window. See Why is the Windows cmd.exe limited to 80 characters wide?

Your problem would be easier diagnosed with a list with some of the elements removed from the end.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Wow, that was weird, I even thought I had checked that... Is it ok if I delete the question? – Antonio Feb 05 '15 at 16:57
  • @Antonio: since I stand to gain from upvotes on my post, I [put it before the Python chatroom community](http://chat.stackoverflow.com/transcript/message/21374539#21374539) to see what they think. I'm on the fence on this one. – Martijn Pieters Feb 05 '15 at 17:00
  • My cmd is configured to be pretty wide, but for the first (and probably last) time in my life I had tried to use "run in interactive mode", which defaults to 80 column. – Antonio Feb 05 '15 at 17:07