Programming with Python · free preview
Debugging
Debug code containing an error systematically. Identify ways of making code less error-prone and more easily tested.
1. Overview
Overview
2. Know What It's Supposed to Do
Know What It's Supposed to Do
3. Make It Fail Every Time
Make It Fail Every Time
4. Make It Fail Fast
Make It Fail Fast
5. Change One Thing at a Time, For a Reason
Change One Thing at a Time, For a Reason
6. Keep Track of What You've Done
Keep Track of What You've Done
7. Version Control Revisited
Version Control Revisited
8. Be Humble
Be Humble
9. Debug With a Neighbor
Debug With a Neighbor
10. Not Supposed to be the Same
Not Supposed to be the Same
11. Fix the accumulator line
A mean function uses total = value inside for value in numbers:. Write the corrected accumulation line.
12. Debug identical BMI outputs and lock the fix with assertions
Run the buggy program and predict why all three outputs are identical. Fix one thing at a time, first the loop index, then the argument order. Run the fixed program and compare the three BMI values; then run the assertions and explain what regression they would catch.
Example. Debug identical BMI outputs and lock the fix with assertions
Problem. Run the buggy program and predict why all three outputs are identical. Fix one thing at a time, first the loop index, then the argument order. Run the fixed program and compare the three BMI values; then run the assertions and explain what regression they would catch.
Correct solution.
Buggy program.```python patients = [[70, 1.8], [80, 1.9], [150, 1.7]]
def calculate_bmi(weight, height):
return weight / (height * height)
for patient in patients:
print(calculate_bmi(patients[0][1], patients[0][0]))
```output
0.000367...
0.000367...
0.000367...Fixed program with tests.```python patients = [[70, 1.8], [80, 1.9], [150, 1.7]]
def calculate_bmi(weight, height):
return weight / (height * height)
def test_bmi():
assert round(calculate_bmi(70, 1.8), 2) == 21.6 assert round(calculate_bmi(80, 1.9), 2) == 22.16 assert round(calculate_bmi(150, 1.7), 1) == 51.9
for patient in patients:
print(round(calculate_bmi(patient[0], patient[1]), 2))
test_bmi()
```output
21.6
22.16
51.9Trace.
- The original
patients[0][1], patients[0][0]uses the first patient's height asweightand the first patient's weight asheightevery iteration. - The fixed loop uses the local
patient, so the input changes on every iteration. - The argument order
weight, heightnow matches the formula, so the outputs are different and plausible. test_bmi()produces no output when all expected rounded results pass.
关键提醒。
The two defects are distinguishable by experiment, changing only the loop source changes the repeated rows, and changing only the argument order changes the scale of every value. The assertions make the corrected behavior reproducible, so a future edit that swaps weight and height again fails immediately.
13. Retrieval practice
A function that should classify [95, 105] as normal prints low for both inputs. Propose two minimal experiments that test different hypotheses, and state the expected output of each experiment after each of the two common causes, a forgotten accumulator update and a reversed comparison.
def classify(readings):
total = 0
for value in readings:
total = value
if total < 100:
return 'low'
return 'normal'
print(classify([95, 105]))Keep each experiment small enough to run in one cell. Identify which experiment result would prove which cause.
14. Mastery check
Debug this new function, which should return the mean of [2, 4, 6] but returns 2.0, total = 0; for value in numbers: with total = value instead of accumulation; then return total / len(numbers). Give the cause, the one-line fix, a regression test, and the exact corrected output.
State the wrong state, the changed line, the test assertion, and the output for the specified list.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free