Raw data, clear context.

[
[
[

]
]
]

Three items are available and five are required. Printing a Boolean tells us whether the quantity is sufficient. This time, give the program a choice: flag a negative quantity, request more stock or confirm there is enough.

Turn a comparison into an action

An if statement tests a condition. Python checks the conditions in order and executes the block belonging to the first true condition, skipping the rest of that chain.[2] elif means “else if”; you can add several elif branches or leave them out. The optional else provides a fallback when every condition is false.[1][2]

Save this as stock-message.py and run it from the same folder with your working Python launcher:

stock-message.py
Python
stock = 3
required = 5
if stock < 0:
print("Invalid stock")
elif stock < required:
print("Restock")
else:
print("Enough stock")
print("Check complete")
Run stock-message.py
Bash
python3 stock-message.py
Output
Plain text
Restock
Check complete

Here, 3 < 0 is false, so Python reaches 3 < 5. That condition is true: Restock is printed and the else block is skipped. The final, unindented line prints Check complete after the choice. This example was tested with Python 3.14.9 on Linux, using no extra packages.

Decision diagram for stock 3 and required 5. The first test, stock < 0, is False, so the second test, stock < required, is reached. It is True and selects Restock. The Invalid stock and Enough stock outcomes remain unselected.
For stock 3 and required 5, the highlighted route passes the negative-stock test and selects Restock. The first true condition ends this chain; the final unindented print still runs.

Make the block visible

Each header ends with a colon. Align if, elif and else, then indent the statements belonging to each branch.[2] Use four spaces for each indented line here. Keep that spacing consistent when editing. Removing the indentation from a branch's print() caused an IndentationError in our test; the script stopped before printing a message.

A common mistake: starting another check

Changing elif to a second if starts a separate decision. With a negative quantity, both checks below succeed:

separate-checks.py
Python
stock = -1
if stock < 0:
print("Invalid stock")
if stock < 5:
print("Restock")
Run separate-checks.py
Bash
python3 separate-checks.py
Output
Plain text
Invalid stock
Restock

The first example keeps those outcomes exclusive. Its negative-stock branch catches -1 before the restocking test is reached. Use that chain when the program should select one message.

Your turn

In stock-message.py, change only stock to -1, 0, 5 and 6, running the file after each edit. Predict the message and check that Check complete follows every time. Which branch handles exactly five items? Next: repeat an action with a while loop.

Sources

  1. Python 3.14 tutorial: if statements
  2. Python 3.14 language reference: compound statements and if