Lessons learned: In this section, we learned about how to use variables in assignment statements, using print statements to visualize our results. We also learned how to use the Python type function to determine and report the data type of an object in a Python program and why the order of operations is important when we have Python code that performs calculations.
Errors
Writing Python code often results in three types of errors: syntax errors, exceptions, and logic errors. We explain and illustrate each of these error types in the following sections.
Syntax Errors
Syntax errors occur when the Python code does not follow the rules that dictate how to write Python code statements. The interpreter identifies syntax errors and highlights the cause of the syntax error in red font in the Python shell window, as shown in Figure 2.7. When a syntax error exists in Python code, the code will not execute until you resolve the syntax error. The cause of the syntax error in this example is that there is nothing combining the string and the variable symbol within the print statement (such as a comma “,”).
Figure 2.7 Python Syntax Error
SCU 2.3 Syntax Errors
Download the file “SCU 2_3.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and execute the program to see that it throws a syntax error when run. Read the text of the error and fix the code as necessary. Execute the modified program after the change to verify that the revised code runs and produces the correct result.
Exceptions
Exceptions occur during Python code execution when you attempt an action that is not possible or not allowed. Figure 2.8 illustrates an example of Python code that results in an exception. Figure 2.9 has the output that corresponds to the execution of the code in Figure 2.8. Due to the fact that the print function was “Print” and not “print,” a NameError exception occurs. The Python interpreter reports that the “name ‘Print’ is not defined,” or in other words, there is no function with that name. Python programmers encounter this type of exception frequently due to Python’s case sensitivity (discussed in Chapter 1).
Figure 2.8 Python Code with Exception
Figure 2.9 Output for Python Code with Exception
Figure 2.10 illustrates another Python code example that results in a different type of exception. Figure 2.11 has the output that corresponds to the execution of the code in Figure 2.10. The exception that occurs is a TypeError exception, which occurs because line 3 of the Python code in Figure 2.10 attempted to add an integer valued variable and a string valued variable together. The Python interpreter reports which line (line 3) was involved and prints out that line in the error message “result = first_number + second_number.” In addition, the explanation is “unsupported operand type(s) for +: ‘int’ and ‘str.’” It has been our experience that beginning Python programmers find these error messages puzzling (and sometimes frustrating), but as they learn the needed terminology and gain experience, the error messages become more helpful and resolving the issues becomes much easier.
Figure 2.10 Python Code with TypeError Exception
Figure 2.11 Output for Python Code with TypeError Exception
Table 2.5 identifies some common Python built-in exceptions. We will see examples of these and other exceptions later in the book. Chapter 5 has an example that uses an installed package to show a variety of exceptions that can occur (and how to resolve them) as Python programs become more involved. References for other Python built-in exceptions are in the official Python documentation (Python Software Foundation, 2019, “Built-in Exceptions”).
Table 2.5
SCU 2.4 Exceptions
Download the file “SCU 2_4.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and execute the program to see that it results in an exception. Remove the quotation marks around the value in the assignment statement in the line indicated to resolve the issue. Execute the modified program to verify that the revised code executes properly and that no exception occurs.
Logic Errors
The third category of errors, logic errors, is often the most difficult to identify. A logic error occurs when a program executes without terminating with an error condition but produces incorrect results. Logic errors can result from using an incorrect operator in an equation or using parentheses in the wrong location. An example of Python code with a logic error is in Figure 2.12.
Figure 2.12 Python Code with Logic Error
Figure 2.13 Output for Python Code with Logic Error
This example demonstrates how