Skip to content
Learn Python with BNHR

Decisions and repetition

On this page
  1. Overview
  2. 4.1 · Household size class
  3. 4.2 · Total members
  4. 4.3 · Heads over an age
  5. 4.4 · The first farm without a crop
  6. 4.5 · Households per barangay
  7. 4.6 · Months to a savings goal
  8. 4.7 · A numbered list

Every exercise in 30_decisions.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 4.1. Household size class

The task

The barangay reports households by size class. Finish size_class(members) so that it returns:

members class
1 "living alone"
2 to 4 "small"
5 to 7 "medium"
8 or more "large"

If the check is red: common causes

  • Wrong at a boundary (4, 5, 7 or 8): compare < and <= with the table: "2 to 4" includes 4.
  • "small" for 1: the test for 1 comes after the test for <= 4. Put it first.
  • None: some number reaches the end of the function without a return. End with else.

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 4.2. Total members

The task

Finish total_members(records) so that it returns the number of people in all the households, from the "members" of each.

call returns
total_members(households) 44
total_members([]) 0

If the check is red: common causes

  • The first household's members only: return is indented inside the loop. Move it out, level with for.
  • The last household's members only: total = 0 is inside the loop.

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 4.3. Heads over an age

The task

Finish heads_over(records, age) so that it returns the IDs of the households whose head is older than age, in the order of the records.

call returns
heads_over(households, 60) ["H002", "H004", "H007"]

If the check is red: common causes

  • Whole records instead of IDs: append record["id"], not record.
  • Wrong when a head is exactly that age: "older than" is >, not >=.

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 4.4. The first farm without a crop

The task

Some farming households did not report their crop. Finish first_missing_crop(records) so that it returns the ID of the first household that farms but whose "crop" is None, or None when there is no such household.

call returns
first_missing_crop(households) "H005"

If the check is red: common causes

  • "H003": a household that does not farm also has no crop. Test record["farms"] as well.
  • The last match instead of the first: the loop keeps going and remembers the latest. Return at the first one.

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 4.5. Households per barangay

The task

Finish per_barangay(records) so that it returns a dictionary with each barangay's number of households.

call returns
per_barangay(households) {"Malanday": 4, "Tumana": 3, "Concepcion Uno": 3}

counts.get(key, 0) gives the count so far, or 0 for a barangay not yet seen.

If the check is red: common causes

  • KeyError for a barangay: its count is read before it exists. Use counts.get(name, 0).
  • Every count is 1: the new count is not written back to the dictionary, or the dictionary is made again inside the loop.

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 4.6. Months to a savings goal

The task

A household saves the same amount every month. Finish months_to_goal(saved, monthly, goal) so that it returns how many months until what is saved reaches or passes the goal. A household that has already reached it needs 0 months.

call returns
months_to_goal(0, 1500, 10000) 7
months_to_goal(12000, 500, 10000) 0

If the check is red: common causes

  • One month too many when the goal is reached exactly: the condition is <=. Stop as soon as saved equals the goal: <.
  • The cell never finishes: saved never changes inside the loop, so the condition stays true. Stop it with ■, then add monthly to saved in the loop.

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 4.7. A numbered list

The task

Finish numbered(names) so that it returns the names numbered from 1, as a list of texts, using a list comprehension with enumerate.

call returns
numbered(["Malanday", "Tumana"]) ["1. Malanday", "2. Tumana"]

If the check is red: common causes

  • Numbered from 0: add start=1 to enumerate.
  • "1.Malanday": the space after the full stop is missing.

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 module
ModuleCheck: Round 2 and the contract
AboutModule 3 of Python for Data Management: write the round's rules once, check every round against them, and clean what can be cleaned as code. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CoursePython for Data Management, free