16 Common Python Runtime Errors 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!')

2) Using = instead of ==. (Causes “SyntaxError: invalid syntax”)

The = is the assignment operator while == is the "is equal to" comparison operator. This error happens with code like this:

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:

spam = ['cat', 'dog', 'mouse']
for i in range(spam):
print(spam[i])

(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:

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.

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

What you probably want to do is this:

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.)

Argh, number 4 is just plain wrong. Why do you encourage people to do the ugly `for i in range(len(spam)):` and then `spam[i]` when `for animal in animals:` and then `animal` is better in every way, and also lets you avoid the opportunity of this error completely?

About number 3, mixing tabs and spaces is a common cause. That happens a lot to people that are learning: copy examples (which tend to use spaces) from a website but modify the code using an editor configured to use tabs.

I like this list a lot, especially as I think everyone forgets quite how much beginners are thrown by bland errors on stuff like tabs/spaces. I think it leads to a lot of the "mein gott! whitespace!" reactions that Python gets.

I wish Python error messages could be a bit more expansive in these cases, or perhaps IDEs could play that role.

Great list, I see these a lot when teaching Python. I find the error messages are really not good enough for those just starting to program. Sometimes they are too vague to be useful like giving "invalid syntax" for a host of different problems. Other times they are confusing to the beginner (what's an EOL?)

Does anyone know how much work it would be to detect these common mistakes and offer clear, nontechnical explanations? I'd be willing to look at it if it's not a huge undertaking.

Hi Al,I have just found your blog.I'm sure you've heard about the Raspberry Pi and it's support for Python. I am one of the writers at the MagPi magazine. We like your books!
markhttp://www.themagpi.com/

Very informative post, but when i was facing java runtime error on my system then i was really very shocked and i think how to fix runtime error. And then i can use so many tool but no any work well on my system for the fixing of this error.