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!
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.
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.
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:
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.
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.
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.
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)?
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:
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.