Skip to content
Learn Python with BNHR

Debugging drill: a report that goes wrong

On this page
  1. Overview
  2. D2.1 · Farmland
  3. D2.2 · The largest household
  4. D2.3 · The average household, by barangay
  5. D2.4 · The harvest
  6. D2.5 · People in farming households

Every exercise in 55_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 D2.1. Farmland

The task

farmland_ha(rows) should add up every household's farm area, given in square metres in the column area_sqm, and give it in hectares (10,000 square metres), to two decimal places.

call should return
farmland_ha(rows) 11.8

Predict. Read the function, then the row shown above: will it run?

If the check is red: common causes

  • KeyError with another name: copy the column's name from the file's first line, area_sqm, letter by letter.
  • TypeError: unsupported operand type(s) for +: 'int' and 'str': the value is text, as every value from a CSV file is. Keep int(...) around 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 D2.2. The largest household

The task

largest_household(rows) should give the row of the household with the most members.

call should return
largest_household(rows) H022's row: 10 members

Predict. Which household will Try it give?

If the check is red: common causes

  • H005: the members are still compared as text. Turn each into a number in the key: int(row["members"]).
  • A number instead of a row: max(int(row["members"]) for row in rows) gives the highest number, 10, not the household. Keep the rows, and give max a key.

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 D2.3. The average household, by barangay

The task

The report gives each barangay's average household. in_barangay(rows, name) picks one barangay's rows, and has no mistake. average_members(group) should give the average number of members of the households in group, to one decimal place.

call should return
average_members(in_barangay(rows, "Malanday")) 4.5
average_members(in_barangay(rows, "Tumana")) 4.1

Predict. Malanday has 11 households with 50 members between them. What will Try it give?

If the check is red: common causes

  • Right for the whole file, too low for a barangay: the function still divides by the number of all the rows, a global, instead of the households it was given, len(group).
  • ZeroDivisionError: the group is empty, because a barangay's name is spelt differently from the file's. Copy it from the file.

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 D2.4. The harvest

The task

harvests.txt has one harvest per line, in tonnes, and Missing where a farm did not report, as in Exercise 6.5. total_harvest(path) should add up the numbers, skip the Missing lines, and round the total to two decimal places.

call should return
total_harvest(PATHS.data / "harvests.txt") 45.55

Predict. Read the function: what will Try it give?

If the check is red: common causes

  • 0 still: the bare except: is still hiding the error. Name the one to skip, except ValueError:, and read what shows up.
  • NameError: name 'flaot' is not defined: that is the hidden error, a misspelt name. It is float.
  • ValueError: could not convert string to float: 'Missing\n': the try is gone. Keep it, with except ValueError:.

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 D2.5. People in farming households

The task

farming_members(rows) should add up the members of the households that farm: those whose farms column says yes.

call should return
farming_members(rows) 106

Predict. Run Try it. Does its answer look like a number of people in farming households?

If the check is red: common causes

  • 137, everyone: any text but "" counts as true. Compare it: row["farms"] == "yes".
  • 0: the comparison is with a value the file does not have, such as "Yes" or True. The file says yes, in lower case.

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 primer
Primer P1Primer: marimo and the Python this course assumes
AboutA primer of Arrays & DataFrames, for any time: how a marimo notebook runs, how the course's exercises work, the Python the course assumes, and how to read an error.
CourseArrays & DataFrames, free