15110 Summer 2017

Lab 5

Goals

The purpose of this lab is to give you practice in developing your debugging techniques to help you solve some issues you may be having in your homeworks. When you are done, you should be able to

Deliverables

  1. debug_part_one.py
  2. debug_part_two.py

Part 1 - Debugging Using Print and Assert Statements

The following activities in Part 1 will use the functions that can be found in the file debug_part_one.py. Copy-paste the code from this file into your own file in IDLE (or gedit) of the same name.

1.1 Class Activity

The following table describes the different types of bugs you may encounter when you are programming:

Syntax Errors Errors At Run Time Logical Errors
Example
 
def print_hello_if_lt5(x):
    if (x < 5) # <--missing colon!
        print("hello")
      
 
def add_strings():
    stringA = "400"
    stringB = 400
    #type mismatch!
    return stringA + stringB 
 
def power(x, exponent):
    for i in range (0, exponent):
        x = x + x**i # <--logical error!
    return x
      
Explanation

A syntax error will fail immediately when Python loads. These are usually the easiest to fix, as long as you know the Python Language syntax. It is simply an error that fails to meet the requirements for formatting the language.

Errors such as the one above can be found when you run the function. These error types such as type mismatches and list boundary errors indicate that you failed to adhere to the Python programming language restrictions but Python can't tell you about the error until it runs the function.

A logical error will run correctly in Python but the function does not behave in the way it is supposed to. For example, the power function is expected to compute:
                        xexponent
but it really computes something like a polynomial of x:
        x + x0 + x1 + x2 + ... + x(exponent-1)
Usually, logical errors are the hardest to find and they can sometimes be very, very subtle. Great care and thought should be taken to avoid them.

1.2 Student Activity

Each of the four functions found in debug_part_one.py have a bug in them. Answer the following questions in the comments above each function definition:

  1. What type of bug did you find? Logical, runtime, or syntax? If syntax, then report which type of syntax error it was.
  2. What line (or in some cases lines) did you find the bug on?
  3. Give a brief description of the bug and why it is wrong.
  4. Give a brief explanation of how you would fix the bug but don't fix it.
  5. If the function has a logical error, use print or assert statements to reveal the error and give an example of when the function does not return the expected result.

When you submit at the end of the lab period, be sure to leave your print or assert statements in the code to show the debugging technique you used to find the bug. Do NOT fix the bug! We want to physically see your debugging technique outline the problem in the code. If you can see the bug from the code without using print or assert statements, be sure to answer every question and still put a print or assert statement in the function that highlights the bug in the function.

Part 2- Writing Test Functions

The following activities in Part 2 will use the functions that can be found in the file debug_part_two.py. Copy-paste the code from this file into your own file in IDLE (or gedit) of the same name.

2.1 Class Activity

2.2 Student Activity

In the file debug_part_two.py, write a test function to find the bugs in the code. Please provide at least 3 test cases that you used to find the logical error in the code. Above the function please give a brief description of the misbehavior of the function and the test case that it failed for. Be sure to check edge cases! Go ahead and fix the function so that your test function finishes without failing an assert statement! However, be careful... just because it passes all your test cases does NOT mean that it is free of bugs.

NOTE: If you do not find the bug in the first three test cases you use, that's okay! Be sure to keep your successful test cases in the test function because it documents what the function is doing correctly. Instead of deleting a test case, keep adding more until you find the bug! This is the glory of test functions. You don't need to manually type the testing into Python, but instead you have Python do it for you!