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
print 3)
- A category of the problem, like
AttributeError
orTypeError
; 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 message
and attach the.py
file that creates the message. The instructor will (eventually) add it to the list.
SyntaxError
- SyntaxError:
invalid syntax
- Python’s way of saying "I am confused". Sometimes even confused about which line was wrong.
- Common Causes
- Missing something (e.g., a
:
,(
,)
,[
, or]
) on this line, generally at the indicated position.
- Have more
(
than)
or[
than]
on a line before the line indicated by Python
TypeError
- 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
expected
numberarguments, got
different number
- E.g.,
math.sqrt()
ormath.sqrt( 1, 2 )
because the function requires exactly one argument.
- TypeError:
unsupported operand type(s) for
operator: '
one type' and '
other type'
- Created when you have an operator with the wrong types. For example,
3 + 'hi'
would sayunsupported operand type(s) for +: 'int' and 'str'
- TypeError:
object of type '
type' has no len()
- The thing inside a
len( ... )
is not a sequence
- TypeError:
a
typeis required
- Created when you have an function argument of the wrong type. For example,
math.sqrt( 'hi' )
would saya number is required
. Note, it does not always get the correct type exactly right.
- TypeError:
int() argument must be a string, a bytes-like object or a number, not
type
- 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'] )
.
ValueError
- Ususally caused when you pass something of the wrong format into a type-converting function.
- ValueError:
invalid literal for int() with base 10:
non-integer string
- Python cannot do
int( 'hi' )
orint( '1.3' )
orfloat( '1 2 3' )
or the like.
AttributeError
- "Attribute" is Python’s generic word for anything following a '.'. For example, the
pi
inmath.pi
or theindex
intext.index( 'hi' )
.
- AttributeError:
'
type' object has no attribute '
name'
- On the indicated line you have something like
x.y
. The type ofx
is the type in the error message,y
is the same as the name in the error message, and things ofx
’s type do not havey
’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 expectedx
to have a different type than it actually has. For example, ifa
is alist
then
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 reassigneda
to the value returned byappend
, which isNone
, not alist
.
IndentationError
- The indicated line does not have the right number of spaces in front of it.
- IndentationError:
unexpected indent
- The indicated line has more spaces in front than does the previous line, and the previous line does not end in a colon.
- IndentationError:
expected an indented block
- The previous line ends in a colon, but the current line is not indented more than the previous line.
- IndentationError:
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
IndexError
- 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 ]
andsomeindex
is out of the legal range, usually meaning the index is greater than or equal tolen( collection )
.
NameError
- Often a typo: you refered to a variable name that does not exist
- NameError:
name '
name' 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
x
to something before the loop.
© 2019 Jim Cohoon | Resources from previous semesters are available. |