Skip to main content

Foundations of Python Programming: Functions First

Section 3.6 Know Your Error Messages

Many problems in your program will lead to an error message. For example as I was writing and testing this chapter of the book I wrote the following version of the example program in the previous section.
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")

current_time_int = int(current_time_str)
wait_time_int = int(wait_time_int)

final_time_int = current_time_int + wait_time_int
print(final_time_int)
Can you see what is wrong, just by looking at the code? Maybe, maybe not. Our brain tends to see what we think is there, so sometimes it is very hard to find the problem just by looking at the code. Especially when it is our own code and we are sure that we have done everything right!
Let’s try the program again, but this time in an activecode:
Aha! Now we have an error message that might be useful. The name error tells us that wait_time_int is not defined. It also tells us that the error is on line 5. That’s really useful information. Now look at line five and you will see that wait_time_int is used on both the left and the right hand side of the assignment statement.

Note 3.6.1.

The error descriptions you see in activecode may be different (and more understandable!) than in a regular Python interpreter. The interpreter in activecode is limited in many ways, but it is intended for beginners, including the wording chosen to describe errors.

Checkpoint 3.6.2.

    Which of the following explains why wait_time_int = int(wait_time_int) is an error?
  • You cannot use a variable on both the left and right hand sides of an assignment statement.
  • No, You can, as long as all the variables on the right hand side already have values.
  • wait_time_int does not have a value so it cannot be used on the right hand side.
  • Yes. Variables must already have values in order to be used on the right hand side.
  • This is not really an error, Python is broken.
  • No, No, No!
In writing and using this book over the last few years we have collected a lot of statistics about the programs in this book. Here are some statistics about error messages for the exercises in this book.
Nearly 90% of the error messages encountered are SyntaxError, TypeError, NameError, ValueError, or AttributeError. We will look at these errors in three stages:
  • First we will define what these four error messages mean.
  • Then, we will look at some examples that cause these errors to occur.
  • Finally we will look at ways to help uncover the root cause of these messages.

Subsection 3.6.1 SyntaxError

Syntax errors happen when you make an error in the syntax of your program. Syntax errors are like making grammatical errors in writing. If you don’t use periods and commas in your writing then you are making it hard for other readers to figure out what you are trying to say. Similarly Python has certain grammatical rules that must be followed or else Python can’t figure out what you are trying to say.
Usually SyntaxErrors can be traced back to missing punctuation characters, such as parentheses, quotation marks, or commas.
Here are a couple examples of Syntax errors in the example program we have been using. See if you can figure out what caused them:

Checkpoint 3.6.3.

Solution.
This is tricky; the error message points us to line 4, but there is no syntax error on that line. The next step should be to back up and look at the previous line. If you look at line 2 carefully you will see that there is a missing right parenthesis at the end of the line. Remember that parentheses must be balanced. Since Python allows statements to continue over multiple lines inside parentheses, python will continue to scan subsequent lines looking for the balancing right parenthesis. However in this case it finds the name current_time_int instead. So the missing parenthesis on line 2 has caused the interpreter to see a syntax error on line 4.

Checkpoint 3.6.4.

Solution.
contains“current time”

Subsection 3.6.2 TypeError

TypeErrors occur when you you try to use an object in a way that is inappropriate for its data type. For example, trying to add together an integer and a string. Usually the line number given by a TypeError error message is an accurate indication of where the problem is.
Here’s an example of a type error created by a Polish learner. See if you can find and fix the error.
Hint: if you’re stuck, it may help to step through this program using Codelens and paying careful attention to the values for x and a

Checkpoint 3.6.5.

Solution.
The error message says that line 5 is trying to do a floor division (//) on a string and number. 24 is definitely a number, so x must be a string. But how? What is the difference between int(x) and x = int(x)?

Subsection 3.6.3 NameError

Name errors often occur when you use a variable before it has a value. Sometimes they are simply caused by typos in your code - for example, you mean to refer to the variable day_of_week but accidentally type day_of_wee. Here is an example of a NameError. See if you can get this program to run successfully:

Checkpoint 3.6.6.

Solution.
The error says the variable wait_time is undefined. Look at line 4 where the variable is declared, and you will see it has been misspelled.
Here is another one for you to try:

Checkpoint 3.6.7.

Solution.
This is another typo, but the typo is not in a variable name, but rather the name of the int function on line 2.

Subsection 3.6.4 ValueError

Value errors occur when a value does not meet a set of expectations or requirements. For example, the int() function can convert a string to an integers, but only if the string actually contains an integer. Attempting int("hello world") will cause a ValueError.
Try running the following program, but when prompted to enter a numeric time, instead enter the word "ten". This will create a value error on line 2, when the program attempts to convert your input to an integer: the value of current_time_str does not meet the requirements of the int() function.
To help avoid Value Errors, make good use of comments in your code, and name your variables in a way that indicates the kind of values they should take.
You have attempted of activities on this page.