A breakpoint can have a condition, so the program stops only when it is true, and the debugger panel follows any expression you type, not only the variables.
scores = [12, 45, 7, 88, 23, 91, 34, 56, 3, 77]
total = 0
best = scores[0]
for i in range(len(scores)):
total += scores[i]
if scores[i] > best:
best = scores[i]
print("Total:", total)
print("Best:", best)
Student
- Right-click (or Shift+click) left of line 5 and enter the condition
i == 6: the breakpoint gets an amber ring. Run stops only in the seventh pass of the loop. - In the panel Watch type
scores[i]and click Add, then addtotal / (i + 1), the average so far. - Click Step a few times: a value that changed is marked. Drag the slider of the timeline back and the expressions show their earlier values.
Teacher
- Works for Python in the browser and for C and C++ on the CodeLab runner 0.20 or newer; nothing to set in the example.
- A condition saves clicking Step fifty times: stop right at the pass where the output goes wrong.