Raw data, clear context.

[
[
[

]
]
]

Three items are available and five are required. Replenish the stock one item at a time, printing the new quantity after each addition. A while loop lets one small block of code do that repeated work.

Check, act, check again

A while statement tests its condition before each repetition. A true result runs the indented body; a false result ends the loop. The body can be skipped entirely if the first test is false.[1]

Save this as restock.py. Run it from the same folder with your working Python launcher:

restock.py
Python
stock = 3
required = 5
while stock < required:
stock = stock + 1
print(stock)
print("Ready")
Run restock.py
Bash
python3 restock.py
Output
Plain text
4
5
Ready

The header ends with a colon, and indentation groups the statements inside the loop.[1][2] Use four spaces for both body lines. The final print("Ready") sits outside the loop, aligned with while.

Here, 3 < 5 is true. The assignment reads the current quantity, adds one and stores 4 back in stock. Then print(stock) shows 4. The next test uses that updated value: 4 < 5 is true, so the body runs again. At 5, the test becomes false and execution reaches Ready.

Trace of restock.py with stock 3 and required 5. Test 3 < 5 is True: increase stock to 4 and print 4. Retest 4 < 5 is True: increase to 5 and print 5. Retest 5 < 5 is False: skip the loop body and print Ready after the loop.
The shown loop tests stock before each repetition. Two True results lead to two updates; the third test is False. Ready is printed after the loop, with stock equal to 5.

Both examples were tested with Python 3.14.9 on Linux, using no extra packages. They simulate quantity changes; they have no delays or connection to a real inventory.

Keep the update inside the loop. With the starting quantity unchanged at 3, this comparison would stay true on every check.

A common mistake: including the boundary

Changing < to <= adds another repetition in this example:

one-too-many.py
Python
stock = 3
required = 5
while stock <= required:
stock = stock + 1
print(stock)
print("Ready")
Run one-too-many.py
Bash
python3 one-too-many.py
Output
Plain text
4
5
6
Ready

At exactly five items, stock <= required still succeeds. The body adds a sixth item before the next check can stop it. Use < for this restocking rule.

Your turn

In restock.py, try starting quantities 5 and 6. Predict whether either run prints a number before Ready, then check. Finally, set stock to 1 and required to 4. Write down every printed line before running it. Next: counted repetition with for and range.

Sources

  1. Python language reference: the while statement
  2. Python tutorial: first steps towards programming