──────────────────────────────────────

44. Reliable Automation · Build a Data Quality Reporter

Separate valid, invalid and duplicate records with a reproducible result.

Lesson files · lessons/reliable-automation/data-quality-report

Before you start
- Reject a missing or invalid field while continuing a loop.
- Track accepted IDs in a set.
- Sum accepted quantities in a running total.

By the end
- Compute totals with separate counts for malformed lines and repeated valid IDs.
- Trace a rejected row and a later valid row with the same ID to explain the report.

Where you can use it
Give the organiser a usable total and separate invalid and duplicate counts so they know which parts of their export need checking.

Give each row a clear route. First check the ID and amount. Once the row is valid, decide whether its ID is already present. Keep separate totals so the person using the report can correct their input before relying on the result.

Predict
Can an invalid amount reserve an ID?
- Yes. It should block a later valid record.
- No. Mark it invalid and keep going.

Worked example (example.py)

rows = ["a,4", "b,zero", "a,4"]
for raw in rows:
    event_id, value = raw.split(",")
    print(event_id)


Example result
a
b
a

Read three lines in ID,amount form. Count invalid lines and duplicate valid IDs. Sum non-negative amounts from the first valid row for each ID. Print Total:, Invalid:, and Duplicates: with their numbers on three lines.

Starter program (starter.py)

rows = ["a,4", "b,zero", "a,4"]
for raw in rows:
    event_id, value = raw.split(",")
    print(event_id)


Hint
Validate the number and nonempty ID before checking the seen set.

Case 1. Try the sample
Input
a,4
b,zero
a,4
Expected output
Total: 4
Invalid: 1
Duplicates: 1

Case 2. Corrected record
Input
a,bad
a,4
b,0
Expected output
Total: 4
Invalid: 1
Duplicates: 0

Case 3. Bad shape
Input
broken
a,4
b,-2
Expected output
Total: 4
Invalid: 2
Duplicates: 0

Project checklist
Run the sample case.

Project checklist
Try one different case and explain what happened.

Marking criterion
Program behaviour (4 points)
Each required behaviour works on the supplied cases.

Marking criterion
Use of the lesson concept (3 points)
The code uses the main idea in a clear, purposeful way.

Marking criterion
Test evidence (2 points)
The reflection describes a case, its expected result and what happened.

Marking criterion
Explanation (1 points)
The learner explains a change or design decision in their own words.

Stretch
Add a result count that reconciles accepted + invalid + duplicate to the number of input lines.

Reflect
Walk through one case where validation order changes the result.

Check your prediction
No. Mark it invalid and keep going. Recording only accepted IDs lets a corrected valid record be counted.
