简体   繁体   中英

Why doesn't this error message point to the first invalid octal number in the list?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM