beginner

Case File: The Fee Table

The finance team has a table of fees: weight bands, express delivery that adds half again, and insurance of two percent with a minimum. Turn it into a small function with default arguments, use it for a list of orders, and check the weights that sit on the edges of the bands. Learn how to write a function that you can trust, call from several places and test on its own. The orders are generated for you alone.

45 min

Sabi Logistics Β· Python 5

The Fee Table

Customers have been quoted three different prices for the same parcel, depending on who answered the phone

Halima Operations manager Β· Phone call

Halima. Customers are being quoted different prices for the same parcel, depending on who picks up the phone. Finance has a fee table, but everybody applies it from memory. I want one function that gives the price, and I want to be able to run it on a whole list of orders and see the total.

The table is on your screen. The part I worry about most is the edges: 1 kilo, 1.1 kilos, exactly 20 kilos, and what insurance costs on a small parcel. If your function gets those right, I will trust it.

The brief

What you need to know first.

  • A function packages a rule: def fee(kg, express=False, insured_value=0): names the function and its inputs; the indented lines are the rule; return base hands the answer back. express=False is a default value: a caller who gives no express value gets False. Defaults make the common case short.
  • Rounding up. math.ceil(kg) (after import math) rounds up to the next whole number: 1.1 becomes 2, 20 stays 20, 20.1 becomes 21. A part of a kilo is billed as a whole kilo.
  • The table. Billable kilos up to 1: 500 naira; up to 5: 900; up to 20: 1800; above 20: 1800 plus 120 for each kilo above 20. Test the bands in order with if ... elif ... else. Express adds half of the base again: base = base + base // 2 (// is division that drops the fraction, which keeps whole naira). Insurance, when the value is more than 0, adds max(100, insured_value * 2 // 100): two percent, but never less than 100.
  • Using the function. price_orders(req) loops over the orders and calls fee(o["kg"], o.get("express", False), o.get("insured_value", 0)) for each; o.get(name, default) reads a key that may be missing without an error. It returns {"fees": [...], "total": ..., "biggest": ...}, with 0 for no orders.
  • Fill in fee, price_orders, then total and biggest. The checks run price_orders on lists that you cannot see: weights of 1, 1.1, 5, 20 and 20.1 kg, a tiny parcel with insurance, and no orders.

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 fee function note

Write the note for the operations manager: the rules of the fee table as your function applies them (rounding up to whole kilos, the bands, the express half, the insurance of two percent with a minimum of 100), the total and the biggest fee for the orders in the case, the edge weights that you checked and their prices (1, 1.1, 5, 20 and 20.1 kg), and how to keep the table up to date safely (the table as data, tests with answers worked out by hand, one function that everybody calls).

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 Fee Table. What would you like to know?
πŸ“

Knowledge Check

1 of 3

What does express=False in a function definition mean?

βš‘ Capture The Flag

Report a problem

About: /lessons/case-the-fee-table