Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer
Чтение книги онлайн.
Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 12
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 easy it is to develop code that executes but does not produce the correct results. Figure 2.13 prints out the message that the average of 4 and 8 is 8! The true average of 4 and 8 is 6 and results when you add the two numbers together prior to dividing the sum by 2. Reviewing the order of operations discussed earlier, we need to put parentheses around the addition of first_number and second_number to correct this error, which adds the numbers together first. As it is now (without those parentheses), we first divide second_number by 2 (resulting in the value 4) and then add that result to first_number (resulting in a final value of 8), which is incorrect. To ensure that code executes with the correct results, programmers need to develop test cases and verify that the expected outcomes for each test case do in fact occur.
SCU 2.5 Logic Errors
Download the file “SCU 2_5.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 runs but does not have the correct result. Add parentheses around the two values being added in the assignment statement in the line indicated to resolve the issue. Execute the modified program to verify that the revised code runs and produces the correct result.
Lessons