Course 2.1Lesson 1 of 5
An error message is not a punishment. It says what went wrong and where. The name of the error is on the last line of the message, and the six to know are ValueError, ZeroDivisionError, IndexError, KeyError, TypeError and NameError. A message of many lines is a traceback, which is read from the bottom: the last line says what, and the lines above show the chain of calls that led to it. A SyntaxError is found before the program runs. A logic fault gives no message at all.
Try the code and the simulations beside the lesson.
After this lesson you can
SyntaxError from an error while running, andYou have seen red messages when something went wrong. They are not a punishment. They tell you what went wrong and where. A good programmer reads them with interest, because they are free help.
The most important part is the name of the error, on the last line.
Predict first. Each line below breaks a rule. Write down the error name that you expect for each.
The program catches every error with try, so it does not stop, and it can show the name and the message of each. (You learn how try works in the next lesson.)
| Error | What went wrong | The line |
|---|---|---|
ValueError | the right type of value, but the content is wrong | int("abc") |
ZeroDivisionError | division by zero | 10 / 0 |
IndexError | a position that does not exist | [1, 2, 3][5] |
KeyError | a key that is not in the dictionary | {"a": 1}["b"] |
TypeError | a value of a type that does not fit | "5" + 5 |
NameError | a name that was never made | unknown_name |
For example, int("abc") is text, which is the right type to give to int, but the content abc cannot become a number. So it is a ValueError. And "5" + 5 mixes text with a number, so it is a TypeError.
When an error stops a program, Python shows a message of several lines. It is called a traceback. It lists the chain of calls that led to the error.
Traceback (most recent call last):
File "main.py", line 9, in <module>
report([])
File "main.py", line 6, in report
avg = average(marks)
File "main.py", line 3, in average
return sum(numbers) / len(numbers)
ZeroDivisionError: division by zeroRead it from the bottom.
ZeroDivisionError: division by zero.return sum(numbers) / len(numbers), inside average.average was called by report, and report was called by the main program.So the cause: report([]) was called with an empty list, and the average of nothing divides by len([]), which is 0. The fault is not in average, it is in the call, or in a missing check for the empty list.
The name of the error is the first clue. It tells you what kind of thing went wrong before you read any further.
Here is a program with a syntax error.
There is a third family that has no message at all: the logic fault. The program runs, and the answer is wrong. No tool can find it for you. You find it by comparing with the answer that you expected.
ValueError and TypeError. Wrong content or wrong type?Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
IndexError by changing one number, then fix it again.ValueError, ZeroDivisionError, IndexError, KeyError, TypeError, NameError.SyntaxError is found before the program runs. A logic fault has no message.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
[1, 2, 3][5] give?10 / 0 give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.