Syntax error and exception messages


Anatomy of a warning

  • The file name and line number like File "error.py", line 7
  • A print of the line in question, like print 3)

Common Python syntax error and exception messages are listed below.


SyntaxError


TypeError


ValueError


AttributeError

a = a.append( 3 )
a = a.append( 4 )

will give the error message 'NoneType' object has no attribute 'append' on the second line because the first line reassigned a to the value returned by append, which is None, not a list.


IndentationError

  • Less than the most recent previous line
  • Not like any other previous line

IndexError


NameError

for i in range( 1, 5 ):
    x = x + i

will have this error if you didn’t set x to something before the loop.

 


 

 

 
   
   
   
 
  © 2019 Jim Cohoon   Resources from previous semesters are available.