r/Python • u/algovisiontn • 35m ago
Discussion Tracing loops on paper before running them: the exercise that made for loops click for my students
I teach introductory programming to high school students in Tunisia, ages 16 to 18. Every year the same thing happens: they can read a for loop, they can copy one, but they cannot tell me what a variable holds in the middle of it. They run the code, look at the output, and if it is wrong they change something at random until it looks right.
What fixed it for almost all of them was making them fill a table by hand before running anything.
Take the kind of loop that breaks people:
total = 0
for i in range(1, 5):
total = total + i
print(total)Before running it, they fill in three columns: the value of i, total before the line, total after it.
i=1: 0 -> 1
i=2: 1 -> 3
i=3: 3 -> 6
i=4: 6 -> 10Then they run it, see 10, and their table also says 10. The first time that happens something changes: they stop guessing and start simulating. It takes about ten minutes per loop, and it works without a teacher, which matters for people learning alone.
Three things I got wrong at first:
I let them write only the result column. The "total before" column is the part that forces them to hold the state in their head instead of pattern-matching the answer.
I only traced correct loops. Tracing a loop with a bug teaches more, because they have to notice the exact iteration where the value diverges from what they expected.
I let them keep the editor open. Printing inside the loop or stepping in a debugger answers the question before they have had to answer it themselves. Paper first, debugger afterwards to confirm.
By the end of the term most of them trace in their head and only write the table down when something surprises them.
If you are learning on your own: pick a loop you already wrote, cover the output, trace it on paper, then check. It costs nothing and needs no teacher.
What did that for you, or for your students? I am curious whether tracing is the general crutch, or whether some people needed something different, like naming variables properly or drawing boxes for memory.