Common Python Runtime Errors Beginners
Common Python Runtime Errors Beginners
Beginners Find
Figuring out what Python's error messages mean can be kind of tricky when you are first learning the
language. Here's a list of common errors that result in runtime error messages which will crash your program.
1) Forgetting to put a : at the end of an if, elif, else, for, while, class, or def statement. (Causes
“SyntaxError: invalid syntax”)
This error happens with code like this:
if spam == 42
print('Hello!')
if spam = 42:
print('Hello!')
3) Using the wrong amount of indentation. (Causes “IndentationError: unexpected indent” and
“IndentationError: unindent does not match any outer indentation level” and
“IndentationError: expected an indented block”)
Remember that the indentation only increases after a statement ending with a : colon, and afterwards must
return to the previous indentation. This error happens with code like this:
print('Hello!')
print('Howdy!')
...and this:
if spam == 42:
print('Hello!')
print('Howdy!')
...and this:
if spam == 42:
print('Hello!')
4) Forgetting the len() call in a for loop statement. (Causes “TypeError: 'list' object cannot be
interpreted as an integer”)
Commonly you want to iterate over the indexes of items in a list or string, which requires calling
the range() function. Just remember to pass the return value of len(someList), instead of passing
just someList.
This error happens with code like this:
(Update: As a few have pointed out, what you might need is just for i in spam: rather than the above
code. But the above is for the very legitimate case where you need the index in the body of the loop, rather
than just the value itself.)
5) Trying to modify a string value. (Causes “TypeError: 'str' object does not support item
assignment”)
Strings are an immutable data type. This error happens with code like this:
6) Trying to concatenate a non-string value to a string value. (Causes “ TypeError: Can't convert 'int'
object to str implicitly”)
numEggs = 12
print('I have ' + numEggs + ' eggs.')
numEggs = 12
print('I have ' + str(numEggs) + ' eggs.')
...or this:
numEggs = 12
print('I have %s eggs.' % (numEggs))
7) Forgetting a quote to begin or end a string value. (Causes “SyntaxError: EOL while scanning string
literal”)
print(Hello!')
...or this:
print('Hello!)
...or this:
myName = 'Al'
print('My name is ' + myName + . How are you?')
8) A typo for a variable or function name. (Causes “NameError: name 'fooba' is not defined”)
This error happens with code like this:
foobar = 'Al'
print('My name is ' + fooba)
...or this:
spam = ruond(4.2)
...or this:
spam = Round(4.2)
9) A typo for a method name. (Causes “AttributeError: 'str' object has no attribute 'lowerr'”)
This error happens with code like this:
10) Going past the last index of a list. (Causes “IndexError: list index out of range”)
This error happens with code like this:
12) Trying to use a Python keyword for a variable name. (Causes “SyntaxError: invalid syntax”)
The Python keywords (also called reserved words) cannot be used for variable names. This happens with
code like:
class = 'algebra'
13) Using an augmented assignment operator on a new variable. (Causes “NameError: name 'foobar' is
not defined”)
Do not assume that variables start off with a value such as 0 or the blank string. A statement with an
augmented operator like spam += 1 is equivalent to spam = spam + 1. This means that there must be a
value in spam to begin with.
This error happens with code like this:
spam = 0
spam += 42
eggs += 42
14) Using a local variable (with the same name as a global variable) in a function before assigning the local
variable. (Causes “UnboundLocalError: local variable 'foobar' referenced before assignment”)
Using a local variable in a function that has the same name as a global variable is tricky. The rule is: if a
variable in a function is ever assigned something, it is always a local variable when used inside that function.
Otherwise, it is the global variable inside that function.
This means you cannot use it as a global variable in the function before assigning it.
This error happens with code like this:
someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
15) Trying to use range() to create a list of integers. (Causes “TypeError: 'range' object does not
support item assignment”)
Sometimes you want a list of integer values in order, so range() seems like a good way to generate this list.
However, you must remember that range() returns a "range object", and not an actual list value.
This error happens with code like this:
spam = range(10)
spam[4] = -1
spam = list(range(10))
spam[4] = -1
(Update: This works in Python 2, because Python 2's range() does return a list value. However, trying to do
this in Python 3 will result in the above error.)
spam = 0
spam++
spam = 0
spam += 1
17) Update: As Luciano points out in the comments, it is also common to forget adding self as the first
parameter for a method. (Causes “TypeError: myMethod() takes no arguments (1 given)”)
This error happens with code like this:
class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()