Answer: Python Puzzler #3870

The answer is B. The program prints:

13 1

This answer was checked by running the program for real.

Why?

// divides and throws away the remainder: each friend gets 13 sweets. % gives the remainder: 1 sweet left over.

Trace table

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

sumanswer
40 // 313
40 % 31

The code again

sweets = 40
friends = 3
print(sweets // friends, sweets % friends)

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