Case File: Late, Very Late, or On Time
Customer service wants every delivery marked on time, late or very late, with a count of each and the worst case. The rule sounds simple and is all edges: exactly on the promised day, late by one, late by three. Learn if and else, comparison operators, and the habit of testing the exact limits, which is where most beginner bugs hide. The deliveries are generated for you alone.
Sabi Logistics · Python 4
Late, Very Late, or On Time
The monthly report said that fifteen parcels were very late, the manager counted twelve, and neither knows which three are different
Emeka Support engineer ·
Emeka. Customer service marks every delivery by hand: on time, late or very late. The monthly report said fifteen parcels were very late. The manager counted twelve. Nobody knows which three are different, and I suspect that it is the ones that are exactly on the edge.
Here is each delivery with the days that were promised and the days that it really took. A delivery that took the promised days or fewer is on time. One that was late by 1 or 2 days is late. One that was late by 3 days or more is very late. Please mark them all, count each mark, and tell me which delivery was worst.
The brief
What you need to know first.
- Comparisons give True or False.
a < b,a <= b,a > b,a >= b,a == b(two equals signs: one equals sign stores a value),a != b. The difference between>and>=is exactly one case, and that case is the edge. - if, elif, else.
if late_by <= 0:runs its indented lines when the test is True;elif late_by >= 3:is tried only if the first test failed;else:catches the rest. Exactly one branch runs. Put the tests in an order where each one is a clear question. - Late by.
late_by = actual_days - promised_days. Zero or less is on time (a day early is still on time). 1 or 2 is late. 3 or more is very late: exactly 3 is very late, not late. - Count, and find the worst. Count the labels with the
counts.get(label, 0) + 1pattern. For the worst, keep the biggestlate_byseen so far starting from 0, and replace it only when a delivery is strictly more late; if nobody was late, the worst isNone. - Fill in
status(parcel),classify(req)returning{"labels", "counts", "worst_id"}, then the variables. The checks runclassifyon lists that you cannot see: exactly on time, late by 1, by 2, by exactly 3, and an empty list.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your data…
Emeka: “Three parcels were late by exactly 3 days. They were counted as late in the report and very late by the manager. Which is right?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Emeka: “So the report and the manager used the same words for different rules.”
What an experienced person would do next. Write the rule in numbers in the report (“very late means 3 days or more after the promised day”) so that everybody means the same by it. Put the limits in named variables at the top of the program (VERY_LATE_DAYS = 3) so that changing the policy means changing one line, and so that the number has a name. Test the edges deliberately: the value just below, on, and just above every limit. If two people count differently, it is nearly always an edge.
The trap. Writing > 3 when the rule says “3 days or more”: every parcel that is late by exactly three moves into the wrong group.
Write the late-delivery note
Write the note for the support engineer: the rule in numbers (on time, late, very late, with the exact edges), how many deliveries fall in each group and which one was the worst, why a report and a manual count can differ by the deliveries that are exactly on an edge (three days is very late), and how to avoid it next time (state the rule in numbers, named limits, tests at the edges).
🧪 Want to practise this on your own computer? See how to set up your lab →