Lesson 12 of 12
Structured learning draftBuild a stock report from validated records
Bring the earlier skills together in a small report. Keep input validation, the reorder rule and presentation separate, then show which items need attention with a predictable order that another person can check.
Learning objectives
- Validate records before reporting.
- Produce a deterministic list of selected items.
- Document how to run and check a small program.
Build a stock report from validated records: the working sequence
Trace the three stages of build a stock report from validated records and compare them with the example.
Each record is checked for a usable name and non-negative integer count.
The reorder rule selects items at or below the supplied threshold.
Sorting makes the displayed order predictable for review and tests.
This final example works on already parsed fictional records. It rejects malformed names and counts before filtering. A CSV reader can feed this function, but the reader must convert numeric text and report bad rows rather than silently skipping them.
def reorder_report(records, threshold):
for item in records:
if not isinstance(item.get('name'), str) or not item['name'].strip():
raise ValueError('Each item needs a name')
if type(item.get('quantity')) is not int or item['quantity'] < 0:
raise ValueError('Quantity must be a non-negative integer')
return sorted(
(item['name'] for item in records if item['quantity'] <= threshold)
)
stock = [{'name': 'Pens', 'quantity': 12}, {'name': 'Books', 'quantity': 3}]
print(reorder_report(stock, 5))['Books']Try it, then change it
- Add an item at quantity 5 and predict its inclusion.
- Try empty records and then a record with a negative quantity.
- Connect your CSV parser and record the filename, input rows and expected report.
Common mistakes
- Silently discarding invalid rows and calling the report complete.
- Depending on incidental input order for the published result.
- Documenting only setup and omitting an example run or test command.
Practice activity
Apply the lesson
Finish the course project: read a fictional CSV, validate its headers and rows, apply a documented reorder rule and print a report. Include tests and a README showing a normal run and an invalid-input run. 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
An invalid row is found while building a stock report. What is the clearest policy for this example?
Lesson summary
- Validate before reporting, rather than hiding malformed records.
- Make output ordering explicit.
- A usable program includes instructions and evidence of its behaviour.
Sources and further reading
- Input and outputPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Course practical outcome
Build a stock report from a fictional CSV file.
Expected output: stock_report.py, a sample CSV, tests and a short README with example output.
Production steps
- Define CSV columns for item name, quantity and reorder level.
- Parse quantities as integers and reject missing names or negative values with a useful message.
- Write a function that finds items at or below their reorder level.
- Test empty data, a boundary quantity, invalid numeric text and a normal record.
- Print a readable report and document how to run the script and tests.
Success criteria
- The same input produces the same ordered report.
- Boundary and invalid inputs have documented behaviour.
- Empty files and bad headers do not produce a misleading success message.
- Tests run with one documented command.
Next step: Ask another learner to run your program with one new CSV fixture, then improve the first confusing error message.
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.