Chapter 3 Debugging

Sections 3.1 - 3.4

  • “You need to organize your programming so that you have lots of little successes.”

  • “Get something working and keep improving it”

  • When debugging, every thing is a suspect except Python.

  • Find and use clues:
    • error messages
    • use print statements
  • Know your error message types - listed in Sec. 3.4. The four most common are:
    • ParseError - syntax related… forgotten parenthesis, common, operator, …
    • TypeError - object non-compatibility… e.g. trying to add an int object to a str object
    • NameError - common cause, using a variable before it is given a value
    • ValueError - error in function parameters… e.g. a function expects a str object as a parameter, but it receives a float object
\(\boxdot\) Further Activities
  • Activity 1: Consider the code shown below. The first line defines a list with variable name “items.” Note that the list consists of objects of different type. This IS valid. Can you identify the type of error within this program? Type the program in an active code window to verify the error type. Then, fix the bug so the program runs correctly.

    items = [1, 2.3, 'orange', 'True', 3.1415, pink, "apple"]
    for item in items:
       print(type(item))  
  • Activity 2: Use the corrected list from the previous Activity, but remove the quote marks from the ‘True’ item. Will this cause an error? Run the program to find out.