Skip to content
Learn Python with BNHR

Functions

On this page
  1. Overview
  2. 5.1 · Yield per hectare
  3. 5.2 · A share, with a default
  4. 5.3 · Households by size, largest first
  5. 5.4 · Yields of the farms
  6. 5.5 · A label for a household

Every exercise in 40_functions.ipynb, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.

Exercise 5.1. Yield per hectare

The task

Finish yield_per_hectare(harvest_kg, area_sqm) so that it returns a farm's yield in tonnes per hectare, rounded to two decimal places. A hectare is 10,000 square metres and a tonne is 1,000 kg.

call returns
yield_per_hectare(2500, 5000) 5.0
yield_per_hectare(4300, 12000) 3.58

If the check is red: common causes

  • Ten times too big or too small: one of the conversions is off. 1 hectare is 10,000 square metres; 1 tonne is 1,000 kg.
  • Many decimal places: round the result to 2.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 5.2. A share, with a default

The task

Finish share(part, whole, decimals=1) so that it returns part as a percentage of whole, rounded to decimals places: one decimal place when the caller does not say.

call returns
share(1, 3) 33.3
share(1, 3, decimals=3) 33.333
share(1, 3, 0) 33.0

If the check is red: common causes

  • Right with two inputs, wrong with three: the body rounds to a fixed 1. Round to decimals.
  • TypeError: share() missing 1 required positional argument: the def line has no default; write decimals=1.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 5.3. Households by size, largest first

The task

Finish largest_first(records) so that it returns the households' IDs from the most members to the fewest. Households with the same number of members keep their order in the records.

call returns
largest_first(households) ["H002", "H008", "H001", "H006", "H003"]

If the check is red: common causes

  • Smallest first: add reverse=True.
  • TypeError: '<' not supported between instances of 'dict' and 'dict': the sort has no key, so it tries to compare whole records.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 5.4. Yields of the farms

The task

Finish farm_yields(records) so that it returns, for every household that farms, its yield per hectare (Exercise 5.1's function, which you can call), in the order of the records.

call returns
farm_yields(households) [5.0, 3.58, 5.5, 7.75]

If the check is red: common causes

  • ZeroDivisionError: a household that does not farm (area 0) is included. Keep only r["farms"].
  • NotImplementedError or a wrong yield: this function uses Exercise 5.1's; finish that one first.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 5.5. A label for a household

The task

The class below describes a household. Its method label is for you to finish: it returns a line for a report with the ID, the members, and whether the household is large, using the class's own is_large. After you write it, run the class's cell again.

call returns
Household("H002", 7, 63).label() "H002: 7 members, large"
Household("H003", 2, 29).label() "H003: 2 members, not large"

If the check is red: common causes

  • NameError: name 'members' is not defined: inside a method, the object's values are self.members and so on.
  • Still not written after you edited it: run the cell with the class again, then this one.
  • TypeError: label() takes 0 positional arguments: the method's first parameter must be self.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Something new

Identify results
LayerExplore a stretch exercises
Stretch exercises S1Stretch exercises
AboutStretch exercises of Python Essentials, after module 6: harder exercises with less help, for when you finish a module early, each with its check and its solution on the companion.
CoursePython Essentials, free