Example Error Messages

Here are a few examples of problems and the error messages they cause. Every example is a code box. Press Run on each one and read what Python says. Most of them fail on purpose, so an error is the expected result. Change the code and run it again to see how the message changes.

An error message names the kind of error on its last line and tells you which line of your code Python was working on. Read that line first.

The last section covers the other kind of mistake: the kind where Python reports nothing at all.

EOF

This code has a mistake, and Python reports an error for it. Run it.

We left out the closing parenthesis. EOF stands for "end of file". Python reached the end of the file without finding the missing parenthesis. Add the ) and run it again.

EOL

Here is another example. Run it.

We left out the closing quotation mark of the string. EOL stands for "end of line". Python reached the end of the line without finding the missing quotation mark. Because a string cannot continue onto the next line, Python stops at the end of the line instead of reading to the end of the file.

NameError

Run this code. Python has never heard of phrase.

We never assigned a value to phrase before trying to print it. Add a line above the print that assigns phrase a string, then run it again.

Check your understanding. Predict the result. Name the error Python reports for the last line.

name: str = "Sam"
print(name)
print(Name)

Python stops with an error on one of these lines. Set error_name to the name of that error, written as a string between quotes, and give it a type hint. Python error names end in Error, and capital letters matter.

Hint 1

Python treats upper and lower case letters as different.

SyntaxError

Run this code.

The left side of an assignment must be a variable name, not a literal value. Swap the two sides, giving age a type hint, and run it again.

TypeError

Run this code and type two numbers when asked. Python stops on the last line.

We did not cast the input strings to numbers before doing math with them. The ** operator does not work on strings. Strings do support + (concatenation), but not **.

Check your understanding. Predict the result. Name the error Python reports for the last line.

age: int = 12
print("Age: " + age)

Python stops with an error on one of these lines. Set error_name to the name of that error, written as a string between quotes, and give it a type hint. Python error names end in Error, and capital letters matter.

Hint 1

The code is written correctly. Look at the types being joined.

Concatenation

In the example above, suppose you changed the ** on the last line to +. Run this version and enter 3 and 4.

There is no error, because + on strings means concatenation. The last line prints 34, which is "3" joined to "4". If you wanted 7, this program is wrong, and Python has no way to tell you.

ValueError and ZeroDivisionError

Two more errors show up once a program reads input and does arithmetic. Run each one and read the last line of the message.

int() is given a string it cannot turn into a number. The type is right (a string), but the value is not. That is a ValueError. Change "twelve" to "12" and run it again.

Dividing by zero has no answer, so Python reports a ZeroDivisionError. Change people to 4 and run it again.

Check your understanding. Predict the result. Each program stops with an error. Name the error for each one.

# first program
count: int = int("3.5")

# second program
share: float = 10 / (5 - 5)

# third program
total: int = "5" + 5

Set first_error, second_error and third_error to the error names, written as strings between quotes, and give each a type hint. Python error names end in Error, and capital letters matter.

Hint 1

int("3.5") gets a string, which is the right type, but the text is not a whole number. The second program divides by something that works out to zero. The third adds a string to a number: is the value wrong, or the type?

Errors Python cannot report

Everything above stopped with a message. Python reports a problem only when it cannot do what you wrote. It cannot know what you meant, so valid code that does the wrong thing runs without any message. These are logic errors. The output looks normal and nothing marks it as wrong except you.

A wrong answer is dangerous because it can be used: a bill, a grade, a dose. The defense is to work out the correct answer yourself, then compare it to what the program prints.

Run this program. It should print the average of 10 and 20, which is 15.

It prints 20.0. Python did exactly what the line says: division comes before addition, so it computed 20 / 2 first and then added 10. The code is valid, so there is no error. The mistake is in the logic.

Run this program. A $50 item with 8% tax should cost $54.

It prints 450.0. Python multiplied by 8, not by 0.08. Again there is no error, only a wrong number. You can catch it only because you know the answer should be close to 54.

Check your understanding. The program below has a logic error. Fix it so that it prints the average of the two numbers, 15.0.

Hint 1

Run the program and compare its output to the answer you get by hand. They differ, so one operation happens in a different order than you intended.

A habit that follows from this: before you trust a program, run it on a small input where you already know the answer, such as 10 and 20. If the output differs, you have found a logic error that Python could not find for you.