Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer. Читать онлайн. Newlib. NEWLIB.NET

Автор: Frederick Kaefer
Издательство: Ingram
Серия:
Жанр произведения: Зарубежная деловая литература
Год издания: 0
isbn: 9781544377452
Скачать книгу
alt="A screenshot displays three lines of code in Python IDLE editor for adding parentheses and operators."/>Description

      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 “,”).

      A screenshot displays the syntax error dialog box in Python.Description

      Figure 2.7 Python Syntax Error

       Stop, Code, and Understand!

      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.

A screenshot displays two lines of code in Python IDLE editor as follows. Line 1: # The following code throws a syntax error. Read the error message and fix the code. Line 2: print “I would like to order a taxi”.

      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).

A screenshot displays two lines of code in a Python file illustrating an exception as follows. Line 2: value_entered = input(“please enter a number between 1 and 100: ”). Line 4: Print(“The value that you entered was: ”, value_entered).

      Figure 2.8 Python Code with Exception

      A screenshot displays the output for Python code with exception.Description

      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.

A screenshot displays three lines of code in a Python file illustrating TypeError exception as follows. Line 1: first_number = 10. Line 2: second_numer = “Ten”. Line 3: result = first_number + second_number.

      Figure 2.10 Python Code with TypeError Exception

      A screenshot displays the output for Python code with TypeError exception.Description

      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”).

       Stop, Code, and Understand!

      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.

      A screenshot displays seven lines of code in a Python file with logic error.Description

      Figure 2.12 Python Code with Logic Error

A screenshot displays the output for a Python code with logic error. The output is titled, RESTART: I:\Fig 2_12 Logic error example.py. There is one line of output as follows. Line 1: The average of 4 and 8 is 8.0.

      Figure 2.13 Output for Python Code with Logic Error

      This example demonstrates how