Syntax error and exception messages
Anatomy of a warning
- Each warning message has three useful parts
- Where Python thinks the problem occured, displayed two ways:
- The file name and line number like
File "error.py", line 7
- A print of the line in question, like
- A category of the problem, like
TypeError; we’ve listed several of the more common categories below
- A description of what Python thinks was the cause of the problem, ranging from the ambiguous (like invalid syntax) to the specific (like
sqrt() takes exactly one argument (0 given)); we’ve listed some of these below too.
Common Python syntax error and exception messages are listed below.
- If you encounter a message not listed, please email the instructor a message with subject line
1112: new Python error messageand attach the
.pyfile that creates the message. The instructor will (eventually) add it to the list.
- Python’s way of saying "I am confused". Sometimes even confused about which line was wrong.
- Common Causes
- Missing something (e.g., a
]) on this line, generally at the indicated position.
- Have more
]on a line before the line indicated by Python
- Indicates that you had a value of the wrong type, or a few related ideas like the wrong number of arguments to a function.
- TypeError: function
arguments, gotdifferent number
math.sqrt( 1, 2 )because the function requires exactly one argument.
unsupported operand type(s) foroperator
: 'one type
' and 'other type
- Created when you have an operator with the wrong types. For example,
3 + 'hi'would say
unsupported operand type(s) for +: 'int' and 'str'
object of type 'type
' has no len()
- The thing inside a
len( ... )is not a sequence
- Created when you have an function argument of the wrong type. For example,
math.sqrt( 'hi' )would say
a number is required. Note, it does not always get the correct type exactly right.
int() argument must be a string, a bytes-like object or a number, nottype
- The various type-conversion functions can work on many types, but not all. Messages like this are created when you give a type that cannot be converted by Python to another type, such as
int( ['1', '3'] ).
- Ususally caused when you pass something of the wrong format into a type-converting function.
invalid literal for int() with base 10:non-integer string
- Python cannot do
int( 'hi' )or
int( '1.3' )or
float( '1 2 3' )or the like.
- "Attribute" is Python’s generic word for anything following a '.'. For example, the
text.index( 'hi' ).
' object has no attribute 'name
- On the indicated line you have something like
x.y. The type of
xis the type in the error message,
yis the same as the name in the error message, and things of
x’s type do not have
y’s in them.
- Common causes
- There are two reasons for this error. The first is simple: you meant to type something other than
y. But possibly more often the error is that you expected
xto have a different type than it actually has. For example, if
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
ato the value returned by
append, which is
None, not a
- The indicated line does not have the right number of spaces in front of it.
- The indicated line has more spaces in front than does the previous line, and the previous line does not end in a colon.
expected an indented block
- The previous line ends in a colon, but the current line is not indented more than the previous line.
unindent does not match any outer indentation level
- This line is indented either
- Less than the most recent previous line
- Not like any other previous line
- You specified an index (usually inside the
after a string or list) that is too large or too small, or a similar error.
- IndexError: type
index out of range
- On the indicated line you have
collection[ someindex ]and
someindexis out of the legal range, usually meaning the index is greater than or equal to
len( collection ).
- Often a typo: you refered to a variable name that does not exist
' is not defined
- The line of code indicated tries to use the variable name indicated but you did not previously initialize the variable.
- One common cause is a misspelling of the actually wanted variable
- Another common cause is an accumulator loop where you failed to initialize the accumulation target:
for i in range( 1, 5 ):
x = x + i
will have this error if you didn’t set
xto something before the loop.