Repository navigation
Expand file tree
/
Copy path06_debug_dataflow.py
More file actions
47 lines (41 loc) · 1.89 KB
/
Copy path06_debug_dataflow.py
File metadata and controls
47 lines (41 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# ============================================================
# CONCEPT: following DATAFLOW to find a bug (debugging lesson)
# ============================================================
# "Dataflow" = how a value is born, changes, and is used.
# This script has a DELIBERATE BUG. The goal of the lesson is to
# find it by watching the data move, not by guessing.
#
# Two ways to watch dataflow:
# A) print-tracing (the print() lines below)
# B) the debugger (set a breakpoint on the marked line, then
# press F5 in VS Code and watch the variables panel)
#
# TASK FOR STUDENTS:
# Run it. It CRASHES with ZeroDivisionError. But the division is
# not the real problem -- it is where the bad data finally bites.
# Use the trace (or a breakpoint) to see WHICH value is wrong and
# WHERE it went wrong, then fix that ONE line.
# ============================================================
scores = [80, 90, 100]
def average(values):
total = 0
count = 0
for v in values:
total = total + v
# count = count + 1 # <-- BUG: this line is commented out!
# TRACE: watch total grow while count stays stuck at 0.
print(f" saw {v:3d} | running total = {total:3d} | count = {count}")
print(f" about to divide: total={total} count={count}") # <-- breakpoint here
return total / count # if count is 0 this even crashes
print("Scores:", scores)
result = average(scores)
print("Average:", result)
# ------------------------------------------------------------
# EXPECTED once fixed: (80+90+100)/3 = 90.0
#
# Teaching point: the bug is NOT on the line that crashes
# (the division). The division is just where the bad data
# ARRIVES. The real cause is upstream, where 'count' should
# have changed but never did. Following dataflow means tracing
# a wrong value back to where it was last correct.
# ------------------------------------------------------------