Debugging drill: values, records and loops¶
Eight functions from the barangay's work, each with one mistake in it, the kind everyone makes: in values (module 2), in records (module 3), and in decisions and loops (module 4). Each check below starts red: that is the point. For each function:
- predict: read the task and the function, and write down what you expect it to give;
- run it, and compare what it gives with what you predicted;
- trace: find the line that makes the difference, in the way the exercise shows;
- fix it, run the function's cell again, and watch the check turn green;
- say what the mistake was, in one sentence: to your table in a training, or in a comment above your fix.
The hints say what you see, never what to change. When you are stuck, the companion has each fix and why it works.
import course
from course import check_cases, try_it
households = [
{"id": "H001", "barangay": "Malanday", "members": 5, "head_age": 47, "farms": True, "crop": "Palay"},
{"id": "H002", "barangay": "Malanday", "members": 7, "head_age": 63, "farms": True, "crop": "Corn"},
{"id": "H003", "barangay": "Tumana", "members": 2, "head_age": 29, "farms": False, "crop": None},
{"id": "H004", "barangay": "Tumana", "members": 1, "head_age": 81, "farms": False, "crop": None},
{"id": "H005", "barangay": "Concepcion Uno", "members": 9, "head_age": 52, "farms": True, "crop": None},
{"id": "H006", "barangay": "Malanday", "members": 4, "head_age": 35, "farms": True, "crop": "Palay"},
{"id": "H007", "barangay": "Concepcion Uno", "members": 3, "head_age": 66, "farms": False, "crop": None},
{"id": "H008", "barangay": "Tumana", "members": 6, "head_age": 44, "farms": True, "crop": "Mango"},
{"id": "H009", "barangay": "Malanday", "members": 5, "head_age": 58, "farms": True, "crop": "Corn"},
{"id": "H010", "barangay": "Concepcion Uno", "members": 2, "head_age": 24, "farms": False, "crop": None},
]
len(households)
10
D1.1 · People from the survey form¶
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.
form_answers = [
{"id": "H001", "members": "5"},
{"id": "H002", "members": "7"},
{"id": "H003", "members": "2"},
{"id": "H004", "members": "1"},
{"id": "H005", "members": "9"},
]
def total_from_form(answers):
"""The number of people in every household the form sent."""
total = 0
for answer in answers:
total = total + answer["members"]
return total
def form_report(answers):
"""The report's line about the form: how many households, and how many people."""
return f"{len(answers)} households, {total_from_form(answers)} people"
Run. The next cell asks for the report's line, and stops with an error. That error is the first thing to read.
form_report(form_answers)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 1 ----> 1 form_report(form_answers) Cell In[2], line 20, in form_report(answers) 18 def form_report(answers): 19 """The report's line about the form: how many households, and how many people.""" ---> 20 return f"{len(answers)} households, {total_from_form(answers)} people" Cell In[2], line 14, in total_from_form(answers) 10 def total_from_form(answers): 11 """The number of people in every household the form sent.""" 12 total = 0 13 for answer in answers: ---> 14 total = total + answer["members"] 15 return total TypeError: unsupported operand type(s) for +: 'int' and 'str'
Trace. Read the error from the bottom up, as module 2, section 3 showed:
- The last line names the kind of error and what it was given:
TypeError: unsupported operand type(s) for +: 'int' and 'str'.+was given a whole number and a text. - Just above it is the line that failed, in
total_from_form, with an arrow under it:total = total + answer["members"].totalstarted as the number0, so the text must beanswer["members"]. - Higher up is how Python got there: your cell called
form_report, which calledtotal_from_form. The mistake is rarely in those lines; they show the way in.
Fix the line that failed, run the functions' cell again, then the cell with the error: it now shows the report's line. Then run the check.
check_cases(
"Drill D1.1", total_from_form,
[("the five households of the form", (form_answers,)), ("one household", ([{"id": "H010", "members": "2"}],)), ("no households", ([],))],
what="forms", hint="Read the error's last line: which two kinds of value did `+` get?",
)
Drill D1.1: not right yet. 1 of 3 forms are right. The first one that is not: the five households of the form. Your code gave "TypeError: unsupported operand type(s) for +: 'int' and 'str'". Read the error's last line: which two kinds of value did + get?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.1
TypeErrorstill: 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.
D1.2 · A crop that was never written down¶
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?
revisit = [
{"id": "H001", "members": 5, "crop": "Palay"},
{"id": "H002", "members": 7, "crop": "Corn"},
{"id": "H003", "members": 2},
{"id": "H004", "members": 1},
{"id": "H008", "members": 6, "crop": "Mango"},
]
def crops(records):
"""Each household's crop, in the order of the records; None for a household without one."""
found = []
for record in records:
found.append(record["crop"])
return found
try_it(crops, revisit)
crops([{'id': 'H001', 'members': 5, 'crop': 'Palay'}, {'id': 'H00…) raised KeyError: "'crop'"
What it usually means: a name was not found: usually a column name that is misspelt, or does not exist yet in the table. Module 2, section 3, shows how to read an error message (open).
Trace. KeyError: 'crop' says a dictionary was asked for a key it does not have. The key is spelt right, and some records have it, so the question is which record does not. Put print(record["id"]) as the first line inside the loop, then run the function's cell and the Try it cell again: the last ID printed before the error is the record without a crop. Take the print out once you have found it.
check_cases(
"Drill D1.2", crops,
[("the revisit's five households", (revisit,)), ("H001 and H002", (revisit[:2],)), ("H003 alone", ([revisit[2]],))],
what="lists of records", hint="Which record does the loop stop at, and what does that record hold?",
)
Drill D1.2: not right yet. 1 of 3 lists of records are right. The first one that is not: the revisit's five households. Your code gave "KeyError: 'crop'". Which record does the loop stop at, and what does that record hold?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.2
KeyError: 'crop'still:record["crop"]fails whenever the key is missing.record.get("crop")givesNoneinstead."none"or""whereNoneis expected: the task asks for the valueNone, which.getgives by itself.- Fewer crops than records: a household without a crop was skipped. Every household needs its place in the list, with
Nonein it.
D1.3 · The oldest heads¶
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?
def oldest(ages, n):
"""The n highest ages, from highest down."""
ages.sort(reverse=True)
return ages[:n]
head_ages = [47, 63, 29, 81, 52]
top_two = oldest(head_ages, 2)
top_two, head_ages[0]
([81, 63], 81)
Trace. oldest gave the right two ages, so nothing looks wrong, until the report reads H001's head and finds 81. Something changed head_ages between the two lines, and only one thing ran there. course.leaves_unchanged (module 3) calls a function on what you give it, and says whether the function changed it:
course.leaves_unchanged(oldest, [47, 63, 29, 81, 52], 2)
oldest changed what it was given (input 1). Build a new value and return it instead; leave the inputs as they were.
def oldest_story(ages, n):
"""What the report sees: the n oldest, then the list it gave, after the call."""
top = oldest(ages, n)
return top, ages
check_cases(
"Drill D1.3", oldest_story,
[("oldest([47, 63, 29, 81, 52], 2), then the list", ([47, 63, 29, 81, 52], 2)), ("oldest([30, 30, 41], 3), then the list", ([30, 30, 41], 3)),
("oldest([25], 1), then the list", ([25], 1))],
what="calls", hint="Look at the list after the call: is it the list you gave?",
)
Drill D1.3: not right yet. 1 of 3 calls are right. The first one that is not: oldest([47, 63, 29, 81, 52], 2), then the list. Your code gave ([81, 63], [81, 63, 52, 47, 29]). Look at the list after the call: is it the list you gave?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.3
- 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 backNone, so there is nothing to slice. Slice whatsorted(...)gives back.
D1.4 · The average household¶
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?
def average_members(records):
"""The average number of members per household, to one decimal place."""
for record in records:
total = 0
count = 0
total = total + record["members"]
count = count + 1
return round(total / count, 1)
try_it(average_members, households)
average_members([{'id': 'H001', 'barangay': 'Malanday', 'members': 5, 'head…) gave back 2.0 (type float).
Trace. 2.0 is H010's members exactly: the average of one household. Make the loop show its work: add print(record["id"], total, count) as the last line inside the loop, run the function's cell and Try it again, and watch total and count from one household to the next. Take the print out once you see it.
check_cases(
"Drill D1.4", average_members,
[("all ten households", (households,)), ("the first three", (households[:3],)), ("H005 alone", (households[4:5],))],
what="lists of households", hint="Watch `total` and `count` as the loop goes: do they grow?",
)
Drill D1.4: not right yet. 1 of 3 lists of households are right. The first one that is not: all ten households. Your code gave 2.0. Watch total and count as the loop goes: do they grow?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.4
- The last household's members:
totalandcountare 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':totalis added to before it is set anywhere. Set it to 0 before the loop.
D1.5 · Heads by age group¶
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.
def age_group(head_age):
"""The age group of a head of household: minor, adult or senior."""
if head_age < 60:
return "adult"
elif head_age < 18:
return "minor"
else:
return "senior"
try_it(age_group, 16)
age_group(16) gave back 'adult' (type str).
Trace. Follow age_group(16) by hand, one line at a time: is 16 < 60? Then which line runs, and does Python look at the conditions below it? Is there any age the elif can catch?
check_cases("Drill D1.5", age_group, [(f"age_group({a})", (a,)) for a in [16, 17, 18, 47, 59, 60, 81]], what="ages",
hint="Which condition does 16 meet first?")
Drill D1.5: not right yet. 5 of 7 ages are right. The first one that is not: age_group(16). Your code gave 'adult'. Which condition does 16 meet first?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.5
"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.
D1.6 · Does every household farm?¶
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?
def all_farm(records):
"""Whether every household in records farms."""
for record in records:
if not record["farms"]:
return False
else:
return True
try_it(all_farm, households[1:4])
all_farm([{'id': 'H002', 'barangay': 'Malanday', 'members': 7, 'head…) gave back True (type bool).
Trace. Put print(record["id"]) as the first line inside the loop, run the function's cell and Try it again: how many households does the function look at before it answers?
check_cases(
"Drill D1.6", all_farm,
[("H001 and H002", (households[:2],)), ("H002 to H004", (households[1:4],)), ("H006 to H010", (households[5:],)), ("H001 alone", (households[:1],)), ("no households", ([],))],
what="lists of households", hint="How many households does the function look at before it returns?",
)
Drill D1.6: not right yet. 2 of 5 lists of households are right. The first one that is not: H002 to H004. Your code gave True. How many households does the function look at before it returns?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.6
Truefor a list with a household that does not farm: the function still returns on the first household, whichever way theifgoes. ReturnTrueonly after the loop, once every household has been seen.Nonefor no households: the loop never runs, and nothing after it returns. End the function withreturn True, after the loop.
D1.7 · Households not visited yet¶
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?
def missing_ids(records, last):
"""The IDs from H001 to the one numbered last, both included, that have no record."""
present = [record["id"] for record in records]
missing = []
for number in range(1, last):
household_id = "H" + str(number).zfill(3)
if household_id not in present:
missing.append(household_id)
return missing
try_it(missing_ids, households, 12)
missing_ids([{'id': 'H001', 'barangay': 'Malanday', 'members': 5, 'head…, 12) gave back ['H011'] (type list).
Trace. The loop takes its numbers from range(1, last). Look at them for yourself: the cell below shows the numbers it gives for 12. Which is the last?
list(range(1, 12))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
check_cases(
"Drill D1.7", missing_ids,
[("the ten households, up to 12", (households, 12)), ("the ten households, up to 10", (households, 10)), ("H001 to H003, up to 5", (households[:3], 5)),
("no households, up to 3", ([], 3))],
what="calls", hint="Which number does `range` stop at?",
)
Drill D1.7: not right yet. 1 of 4 calls are right. The first one that is not: the ten households, up to 12. Your code gave ['H011']. Which number does range stop at?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
If the check is red: common causes for Drill D1.7
- The last ID is never there:
rangestops before its second number. To includelast, go up tolast + 1. H000in the list: the range starts at 0. IDs start at 1.
D1.8 · Heads aged 60 or more¶
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?
def seniors(records):
"""The IDs of the households whose head is 60 or older."""
return [record["id"] for record in records if record["head_age"] > 60]
try_it(seniors, households)
seniors([{'id': 'H001', 'barangay': 'Malanday', 'members': 5, 'head…) gave back ['H002', 'H004', 'H007'] (type list).
It looks right. On the barangay's ten households it gives exactly the table's answer: no error, and nothing to trace. Run its check anyway.
check_cases(
"Drill D1.8", seniors,
[("all ten households", (households,)), ("a head aged exactly 60", ([{"id": "H031", "head_age": 60}],)),
("heads aged 59, 60 and 61", ([{"id": "H032", "head_age": 59}, {"id": "H033", "head_age": 60}, {"id": "H034", "head_age": 61}],))],
what="lists of households", hint="The check names the first case that is not right. What does the task say about that age?",
)
Drill D1.8: not right yet. 1 of 3 lists of households are right. The first one that is not: a head aged exactly 60. Your code gave []. The check names the first case that is not right. What does the task say about that age?
Still stuck after ten minutes? The solution and why it works are on the companion ↗.
Trace. The check names the first case that is not right: a head aged exactly 60. Read the task's words for that age, then the condition in the code.
If the check is red: common causes for Drill D1.8
- 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.
What the eight mistakes have in common¶
Only two stopped with an error, and that error said where to look: read from the bottom up, its last line named the kind of mistake and the line above it the place. The other six ran, and gave a wrong answer without a word: a list in a new order, an average of one household, a minor called an adult, a yes after one look, an ID never looked at, a senior left out. The last gave the right answer on the barangay's own records. That is why every function in this course has a check, and why the checks try the edges.
Next: Functions, in 40_functions.ipynb.