Answer: Python Puzzler #2062

The answer is A. The program prints:

Time to recharge!

This answer was checked by running the program for real.

Why?

Python checks the conditions from the top and runs the first one that is true. Is 0 > 15? No. Is 0 > 6? No. Neither was true, so the else part runs.

Trace table

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

questionanswer
battery > 15False
battery > 6False

The code again

battery = 0
if battery > 15:
    print("Full speed ahead!")
elif battery > 6:
    print("Slow and steady.")
else:
    print("Time to recharge!")

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