Answer: Python Puzzler #2501

The answer is B. The program prints:

26

This answer was checked by running the program for real.

Why?

range(2, 12, 3) starts at 2, goes up in steps of 3, and stops before 12. So the loop adds 2 + 5 + 8 + 11. The total is 26.

Trace table

A trace table follows the program step by step. Here’s this one:

numbertotal after the loop body
22
57
815
1126

The code again

total = 0
for number in range(2, 12, 3):
    total = total + number
print(total)

Try it: change one number in the program and predict the new output before you run it.