Facing up to mistakes

INPUT · Slides

Telling errors apart by name

01 / 07

An error is a report

Run your code, get a wall of unfamiliar text back, and it can be unnerving. But nobody is telling you off — it is a report from Python.

The important part of the report is the last line. It comes in the shape kind: body, as in NameError: name 'nane' is not defined.

In this lesson, learn the names of the kinds and nothing else. Once a name alone is enough to make you think "ah, that one", the time you spend stuck drops sharply.

02 / 07

NameError: a name it does not know

This is what you get when you use a name Python does not know. Nearly always it is a typo, or a variable used before it was made.

In the example below the name was made as name and then called nane. Python is honest about it: "nobody ever mentioned a nane to me".

is not defined means exactly that. Suspect your spelling, and the order you did things in.

name = "Yui"print(nane)

Result

NameError: name 'nane' is not defined. Did you mean: 'name'?

03 / 07

TypeError: the wrong kind

You get this when you combine things of kinds that do not go together. Joining text to a number with + is the classic.

Python takes the view that the only thing you can add to text is more text, so 300 will not go on the end as it stands. str(300) turns it into text, and an f-string does the same job.

print("price: " + 300)

Result

TypeError: can only concatenate str (not "int") to str

04 / 07

IndexError: past the end

Reach for a position a list has not got and you get this. With three items the positions you can use are 0, 1 and 2. There is no 3.

list index out of range means "that position is past the end". Remember it as up to one less than len().

fruits = ["apple", "orange", "grape"]print(fruits[3])

Result

IndexError: list index out of range

05 / 07

KeyError: a key the dictionary has not got

Ask a dictionary for a key that is not in it and you get this. The body carries the key you asked for, so you can see at once which one was wrong.

A list gives you IndexError, a dictionary gives you KeyError. The kind of container changes the name of the error — a neat detail.

prices = {"apple": 120, "orange": 80}print(prices["grape"])

Result

KeyError: 'grape'

06 / 07

ValueError: the right kind, the wrong contents

The kind is fine, but the contents are not in a workable shape. int("abc") is the standard example.

int() turns text into a number. Handing it text is entirely correct — but "abc" is not shaped like a number, so you are told that this particular value will not do.

TypeError is the wrong kind; ValueError is the right kind with the wrong contents. That is how you tell them apart.

print(int("abc"))

Result

ValueError: invalid literal for int() with base 10: 'abc'

07 / 07

Six will see you through for now

There is one more worth meeting: ZeroDivisionError, which turns up when you divide by zero. Working out a share per head when the head count is zero is the usual way in.

Here are the names against their causes.

NameWhat happened
NameErroryou used a name it does not know
TypeErroryou combined kinds that do not go together
IndexErroryou reached past the end of a list
KeyErroryou asked a dictionary for a key it has not got
ValueErrorthe kind was right, the contents were not
ZeroDivisionErroryou divided by zero

Those six account for most of what a beginner produces. Next comes the wall of text that arrives alongside this one line, and how to read it.

print(10 / 0)

Result

ZeroDivisionError: division by zero