Example Error Messages
Quiz: counts toward finishing the lesson. Check: just for practice.
Here are a few examples of problems and the error messages they cause. The snippets below are not runnable cells, because most of them fail on purpose. Read each one and notice what Python reports.
EOF
This code has a mistake, and Python reports an error for it:
print("hello"
We left out the closing parenthesis. EOF stands for "end of file". Python reached the end of the file without finding the missing parenthesis.
EOL
Here is another example:
print("hello)
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
print(phrase)
We never assigned a value to phrase before trying to print it.
Check your understanding. Predict the result. Name the error Python reports for the last line.
name = "Sam"
print(name)
print(Name)
Python stops with an error on one of these lines. Change error_name to the name of that error, written as a string, for example "ZeroDivisionError".
Hint 1
Python treats upper and lower case letters as different.
SyntaxError
32 = age
The left side of an assignment must be a variable name, not a literal value.
TypeError
num_1 = input("Enter first operand: ")
num_2 = input("Enter second operand: ")
print("The result is", num_1 ** num_2)
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 = 12
print("Age: " + age)
Python stops with an error on one of these lines. Change error_name to the name of that error, written as a string, for example "ZeroDivisionError".
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 +, so
that the line looked like this:
print("The result is", num_1 + num_2)
This would cause no error because + on strings means concatenation. If you
entered 3 for num_1 and 4 for num_2, the last line would print 34, which is
"3" joined to "4".