beginner

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.

45 min

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, with p holding the current one. To add up: total = 0, then inside the loop total = total + p["kg"]. The shortcuts are len(parcels) for the count and sum(...) 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 an if 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 and return to give the answer), then the four variables total_kg, average_kg, heaviest_id and over_limit from 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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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.

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Case File: The Weigh-In. What would you like to know?
📝

Knowledge Check

1 of 3

What does len(parcels) give?

⚑ Capture The Flag

Report a problem

About: /lessons/case-the-weigh-in