Case File: The Weigh-In
Your first case file. A depot has weighed this morning's parcels. Read the file with json.load, add up the weights, find the average and the heaviest parcel, list the parcels that are over the limit, and make the answers right for an empty day and for a parcel of exactly the limit. Learn variables, numbers, lists, dictionaries and the loop by doing a real small job. The data is generated for you alone.
Sabi Logistics · Python 1
The Weigh-In
Every morning somebody adds up the weights by hand and sometimes gets the special van wrong
Halima Operations manager ·
Halima. Every morning at the Abuja depot somebody adds up the weights of the parcels by hand and decides which need the special van. Last week the sum was wrong and a heavy parcel went on a small van.
I have the list in a file: an id and a weight for every parcel, and the limit above which a parcel needs the special van. Could you write a small program that gives me the count, the total weight, the average, the heaviest parcel and the list of parcels over the limit? It must also work on a day with no parcels, because on Sundays that happens, and the program must not crash.
The brief
What you need to know first.
- Variables and types. A variable is a name for a value:
count = 12(a whole number),rate = 0.25(a number with a decimal point),name = "Kano"(text),ok = True(true or false),nothing = None(no value yet). You do not declare the type: Python knows it from the value. - Reading the file.
req = json.load(open("parcels.json"))opens the file and turns it into Python objects. A JSON list becomes a list (req["parcels"]is a list of parcels) and a JSON object becomes a dictionary (p["kg"]is the value stored under the name “kg”). - A loop.
for p in parcels:runs the indented lines once for each parcel, withpholding the current one. To add up:total = 0, then inside the looptotal = total + p["kg"]. The shortcuts arelen(parcels)for the count andsum(...)for adding. - Two edges to remember. The average is
total / count, but with no parcels the count is 0 and a division by zero crashes: decide what the answer is (here 0) and write anif count else 0. And “over the limit” is strictly more than the limit: a parcel of exactly 20 kg is fine. - Fill in
summary(req)(a function:def name(inputs):with the lines indented under it andreturnto give the answer), then the four variablestotal_kg,average_kg,heaviest_idandover_limitfrom its result. Press Run to try your code and Check my answers to be judged. The checks run your function on lists that you cannot see: always write the rule, never the answer.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your data…
Halima: “Perfect. Now they want it to say which parcels are over by more than 5 kg as well. Do I have to ask you again?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Halima: “So with the file in your program, the next question is another few lines.”
What an experienced person would do next. Put the limit and the numbers in the data, not in the code, so that the next question changes a file and not a program (here the limit was already in the file). Print what you computed (print(total_kg)) while you are learning, to see that each step is what you expected. Learn the three questions that every case asks: what does it do on empty input, what does it do on an exact limit, and what does it do on a tie. Those three produce most of the bugs that you will meet, in Python and everywhere else.
The trap. Dividing by a count that can be zero, and writing > where the rule says “at least” (>=), or the other way round.
Write the weigh-in note
Write the note for the operations manager in plain words: what your program calculates (count, total, average, heaviest parcel, parcels over the limit), the numbers it found for this morning, what it does on a day with no parcels and why (a division by zero would crash it), how it treats a parcel of exactly the limit and a tie for the heaviest, and how you checked your answers.
🧪 Want to practise this on your own computer? See how to set up your lab →