Exception and Traceback
00:36 Note that the traceback message locates the error in line 5, not line 4. The Python interpreter is attempting to point out where the invalid syntax is. However, it can only really point to where it first noticed the problem.
But once the interpreter encounters something that doesn’t make sense, it can only point you to the first thing it found that it couldn’t understand. There are a few elements of a
SyntaxError traceback that can help you determine where the invalid syntax is in your code.
Firstly, the filename where the invalid syntax was encountered. Secondly, the line number and reproduced line of code where the issue was encountered. Thirdly, a caret (
^) on the line below the reproduced code, which shows you the point in the code that has the problem.
These exceptions both inherit from the
SyntaxError class, but they’re special cases where indentation is concerned. An
IndentationError is raised when the indentation levels of your code don’t match up.
Become a Member to join the conversation.