Editor

Predict the Output

Beginner · Predict the output · from Python While Loops: When For Loops Aren't Enough

From the lesson: Python While Loops: When For Loops Aren't Enough

A vending machine doesn't know how many coins you'll insert. It just keeps waiting: "while the total is less than the price, keep accepting coins." It can't use a for loop because it doesn't know the number of iterations in advance. It needs a while loop.

Read the lesson

Without running the code, predict exactly what it will print. Trace through each iteration carefully, paying attention to when the condition is checked and when x changes.

Then run it to check your answer.

x = 10
while x > 1:
x = x // 2
print(x)
print("done")

More While Loops exercises

All Python exercises · 50 practice problems · Control Flow quiz