Answer: Python Puzzler #1347

The answer is B. The program prints:

42
What the program prints
Checked by running the program for real.

Why?

The loop remembers the biggest number it has seen so far, and replaces it whenever it finds a bigger one. This is how computers find the maximum.

Trace table

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

nn > biggest?biggest
24yes24
42yes42
31no42
28no42
15no42

The code again

numbers = [24, 42, 31, 28, 15]
biggest = 0
for n in numbers:
    if n > biggest:
        biggest = n
print(biggest)

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