Using the debugger
A debugger pauses your program at a line so you can read its variables, instead of guessing and adding prints.
The moves
- Breakpoint: pause when execution reaches this line.
- Step over: run this line, then pause on the next.
- Step into: follow a call into the function.
- Continue: run until the next breakpoint.
In Pythilo's debug panel, tap a line number to set a breakpoint. On a desktop, python -m pdb file.py gives the same moves with n, s, c and p name, and a breakpoint() call in your code pauses at that spot.
A way to work
Choose an input that fails. Say what you expect each variable to hold, step, and compare. The bug is at the first line where reality differs from your expectation.
def mean(xs):
total = 0
for x in xs:
total = x # pause here
return total / len(xs)Watching total here shows the bug on the second pass.
Your task
moving_average(values, window) should return the mean of every consecutive run of window values. It has two bugs. Find them by stepping through moving_average([1, 2, 3, 4], 2), then fix them.
The explanation opens here once every check passes.