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.
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: |
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:
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.
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.
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!