Python, properly · Chapter 13 of 42

Functions

By the end of this chapter you can write your own functions, give them inputs, get a value back with return, document them, build bigger ones from smaller ones, and follow a traceback through several of them. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data and functions
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

The delivery fee rule lives in three scripts now: the bill, the website quote and Rafiq's month-end report. On Monday Farah changed it, and Rafiq found the report still charging the old fee. The same few lines, copied three times, drift apart.

A function gives a piece of logic a name, so it is written once and used everywhere: change it in one place and every caller gets the change. It is also how you test code (from chapter 21 on, every check is a test of a function) and how every library you will use is built. From this chapter on, most of your tasks are functions that the check calls with its own inputs.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    delivery_fee.py: one rule, one place

    Write delivery_fee(km): it returns the delivery fee in rupees, as a whole number, for a delivery of km kilometres. Up to and including 2 km is free (0), up to and including 5 km is 30, and anything further is 50.

    The check calls it, for example delivery_fee(1.5) returns 0, delivery_fee(4) returns 30 and delivery_fee(8) returns 50, and tries exactly 2 and exactly 5 too. Return the fee: do not print it.

  2. Task 2

    bill_line.py: a line of the bill, returned

    Write bill_line(name, price, quantity): it returns (not prints) one line of the bill as a string, with the amount worked out. For example bill_line("Roti", 15, 4) returns:

    Roti (4 x Rs 15): Rs 60

    Give it a docstring, a first line in the body saying what it returns; the check reads that too.

  3. Task 3

    order_total.py: a function built from a function

    Orders store their items like roti:2;dal-tadka:1. Write two functions; both get prices, a dictionary of dish id to price, as an argument.

    item_total(item, prices) returns the cost of one item: item_total("roti:2", prices) is 30 when roti costs 15.

    order_total(items, prices) returns the total of a whole order by calling item_total for each item: order_total("roti:2;dal-tadka:1", prices) is 170 when dal-tadka costs 140. An order with no items is the empty string and totals 0.

  4. Task 4 · interview question

    Interview: fix_total.py, the wrong answers back

    fix_total.py has two functions with bugs. subtotal(prices) should return the sum of a list of prices, and with_delivery(prices, fee) should return that subtotal plus the fee.

    Right now subtotal([140, 15, 90]) returns 140 instead of the sum, subtotal([]) returns None, and with_delivery returns None. Fix both. Keep the loop in subtotal (no sum), and keep with_delivery calling subtotal.

What you will learn

  • def: defining a function, its parameters, and calling it.
  • return: handing a value back, and the trap of printing instead.
  • Docstrings: saying what a function does, where Python can read it.
  • Functions calling functions, the call stack, and a traceback that runs through several functions.

More in Data and functions

Build "Functions" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.