Skip to content
Learn Python with BNHR

Debugging drill: values, records and loops

On this page
  1. Overview
  2. D1.1 · People from the survey form
  3. D1.2 · A crop that was never written down
  4. D1.3 · The oldest heads
  5. D1.4 · The average household
  6. D1.5 · Heads by age group
  7. D1.6 · Does every household farm?
  8. D1.7 · Households not visited yet
  9. D1.8 · Heads aged 60 or more

Every exercise in 35_debugging_drill.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.

Drill D1.1. People from the survey form

The task

The enumerators' survey form sends each household's answers as text, as a form always does: "5", not 5. total_from_form(answers) should add up the members of every household the form sent, and form_report(answers) puts that total in a line for the report.

call should return
total_from_form(form_answers) 24
form_report(form_answers) "5 households, 24 people"

Predict. Read both functions: what will the cell after them show? Write it down before you run anything.

If the check is red: common causes

  • TypeError still: the answer is still added as the text it is. Turn it into a whole number where it is read: int(...).
  • A text of digits, such as '057219': the total was made text, so + joined the answers instead of adding them. Keep the total a number, and turn each answer into 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.

Drill D1.2. A crop that was never written down

The task

On the revisit, the enumerators wrote down a household's crop only when it had one: the records of households without a crop have no "crop" key at all. crops(records) should give each household's crop, in the order of the records, and None for a household without one.

call should return
crops(revisit) ["Palay", "Corn", None, None, "Mango"]

Predict. Will it give the list in the table? If it stops, at which household?

If the check is red: common causes

  • KeyError: 'crop' still: record["crop"] fails whenever the key is missing. record.get("crop") gives None instead.
  • "none" or "" where None is expected: the task asks for the value None, which .get gives by itself.
  • Fewer crops than records: a household without a crop was skipped. Every household needs its place in the list, with None in it.

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.

Drill D1.3. The oldest heads

The task

head_ages holds the heads' ages in the order of the households, H001's first. oldest(ages, n) should give the n highest ages, from highest down, and leave the list it is given as it was: the report reads H001's head from it afterwards.

call should return
oldest([47, 63, 29, 81, 52], 2) [81, 63]

Predict. The cell after the function calls oldest(head_ages, 2), then shows head_ages[0], H001's head. What will each be?

If the check is red: common causes

  • Right ages, but the list after the call is sorted: the function still sorts the list it was given. .sort() changes a list; sorted(...) makes a new one.
  • TypeError: 'NoneType' object is not subscriptable: .sort() gives back None, so there is nothing to slice. Slice what sorted(...) gives back.

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.

Drill D1.4. The average household

The task

average_members(records) should give the average number of members per household, to one decimal place.

call should return
average_members(households) 4.4
average_members(households[:3]) 4.7

Predict. What will Try it give? Then look at the last household of the list: how many members does it have?

If the check is red: common causes

  • The last household's members: total and count are still set to 0 inside the loop, so each household starts them again. Set them once, before the loop.
  • UnboundLocalError: cannot access local variable 'total': total is added to before it is set anywhere. Set it to 0 before 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.

Drill D1.5. Heads by age group

The task

The barangay reports heads of household by age group: minor under 18, adult from 18 to 59, senior from 60.

call should return
age_group(16) "minor"
age_group(47) "adult"
age_group(63) "senior"

Predict. What will age_group(16) give? Read the function from the top, as Python does.

If the check is red: common causes

  • "adult" for 16 or 17: the test for adults still comes first and catches them. Test the narrowest case, under 18, first.
  • Wrong at 18 or 60: compare < and <= with the groups: 18 is an adult, 60 a senior.

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.

Drill D1.6. Does every household farm?

The task

A programme for farmers needs to know whether every household on a list farms. all_farm(records) should give True when every household farms, and False when one or more does not. A list with no households has none that does not farm, so it gives True.

call should return
all_farm(households[:2]) True
all_farm(households[1:4]) False

Predict. H002 farms; H003 and H004 do not. What will all_farm(households[1:4]) give?

If the check is red: common causes

  • True for a list with a household that does not farm: the function still returns on the first household, whichever way the if goes. Return True only after the loop, once every household has been seen.
  • None for no households: the loop never runs, and nothing after it returns. End the function with return True, after 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.

Drill D1.7. Households not visited yet

The task

Household IDs run from H001 up. missing_ids(records, last) should give the IDs from H001 to the one numbered last, both included, that have no record yet, in order.

call should return
missing_ids(households, 12) ["H011", "H012"]

Predict. The ten households are H001 to H010. What will the function give for 12?

If the check is red: common causes

  • The last ID is never there: range stops before its second number. To include last, go up to last + 1.
  • H000 in the list: the range starts at 0. IDs start at 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.

Drill D1.8. Heads aged 60 or more

The task

The office for senior citizens wants the IDs of the households whose head is 60 or older, in the order of the records.

call should return
seniors(households) ["H002", "H004", "H007"]

Predict. Run Try it: does it give what the table says?

If the check is red: common causes

  • Red for a head aged exactly 60: "60 or older" includes 60, which is >=. > leaves it out.
  • Whole records instead of IDs: keep record["id"] in the list.

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 debugging drill
Debugging drill D1Debugging drill: a table that lies
AboutThe first debugging drill of Arrays & DataFrames, after module 3: six bugs in reading and selecting the farm survey, to predict, trace, fix and explain.
CourseArrays & DataFrames, free