Lesson 3 of 12
Structured learning draftMake a decision with if and elif
A stock alert has three outcomes: invalid quantity, reorder needed, or stock available. Write the branches in a deliberate order and test the boundary where the rule changes, rather than trying only an obvious example.
Learning objectives
- Write an ordered decision chain.
- Use a comparison at a threshold.
- Test each branch and the equality case.
Make a decision with if and elif: the working sequence
Trace the three stages of make a decision with if and elif and compare them with the example.
Compare the quantity with the invalid-value rule first.
If valid, check whether it is at or below the reorder threshold.
Quantity equal to 5 follows the reorder branch because the comparison includes equality.
Python checks an if/elif chain in order and runs the first matching branch. Indentation identifies the statements in each branch. Put invalid values first so they cannot be reported as an ordinary reorder.
quantity = 5
reorder_level = 5
if quantity < 0:
print('Invalid quantity')
elif quantity <= reorder_level:
print('Reorder needed')
else:
print('Stock available')Reorder neededTry it, then change it
- Run quantities -1, 4, 5 and 6 separately.
- Write down the expected branch before each run.
- Change <= to < and identify which single test changes its result.
Common mistakes
- Placing a broad condition before a specific invalid-value check.
- Using = instead of == when comparing values.
- Changing indentation without understanding the branch it affects.
Practice activity
Apply the lesson
Write a delivery rule: quantities below 0 are invalid, 0 means no dispatch, and positive quantities mean ready to dispatch. Test -1, 0 and 1. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
With quantity = 5 and reorder_level = 5, which comparison includes the boundary?
Lesson summary
- Branch order affects the result.
- Threshold rules need an equality test.
- Cover every branch with a known input.
Sources and further reading
- Control flow: if statementsPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.