r/learnpython • u/algovisiontn • 16m ago
How do you get beginners to trace nested loops by hand? Mine can write loops but fail "what does this print" questions
I teach algorithmics and Python to high-school students (Tunisia, ages 16-18). Their exams are on paper, so they have to predict the output of a program without running it.
The pattern I keep seeing: students can write a single loop fine, but as soon as there is a nested loop with a counter or an accumulator changing inside, they guess instead of tracing. Typical example:
total = 0
for i in range(1, 4):
for j in range(i):
total += i * j
print(total)
Most of them get it wrong, and when I ask them to show their steps they have none.
What I have tried so far: a trace table with one column per variable and one row per iteration, and making them write the expected output before running the code. It helps the stronger students. The weaker ones still skip the table.
What works for you? Specifically:
- Is there a way to make the trace table feel less like extra work?
- Do you start with nested loops, or build up from a single loop with a changing accumulator?
- Any exercise types that made tracing stick?
Thanks.
•
u/ResidentPomegranate6 14m ago
make them expand range(i) into literal values first, so each outer iteration becomes one row of a grid. summing the (i,j) cells is less abstract than a variable table and still fits on paper.