Every exercise in 90_primer_marimo.py, 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 P1.1. One line about a household
The task
Why it matters: this is the whole rhythm of the course's exercises, on Python you know: a dictionary, an f-string and return.
Input → output: a household as a dictionary with "hh_id", "province" and "hh_size" → one line of text about it.
household |
returns |
|---|---|
{"hh_id": "HH-0001", "province": "Ilocos Norte", "hh_size": 7} |
"HH-0001, Ilocos Norte, household size 7" |
{"hh_id": "HH-0412", "province": "Iloilo", "hh_size": 1} |
"HH-0412, Iloilo, household size 1" |
Tools: household["hh_id"] looks up a value by its key; an f-string puts values into text (section 4).
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
None |
The function prints the line instead of returning it. | return f"..." |
| Text that differs by a comma or a space | The f-string's fixed parts differ from the examples. | ", " after the ID and the province, household size before the size |
{household['hh_id']} in the result, braces and all |
The f in front of the text is missing, so nothing is filled in. |
f"..." |
| KeyError: 'HH_ID' | A key spelled differently from the dictionary's. | "hh_id", "province", "hh_size" |
| The same text for every household | The values are typed in instead of looked up. | household[...] for each value |
Still stuck after ten minutes? Ask your table, then raise your hand.
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.
The idea
Look the three values up by their keys and put them into one f-string, with the commas, spaces and words exactly as in the examples; then return it.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def household_line(household: dict) -> str: """One line of text about a household: its ID, its province and its size.""" # Exercise P1.1: the task and examples are in the text above this cell. return f"{household['hh_id']}, {household['province']}, household size {household['hh_size']}"Why each part is there
household["hh_id"]and the other two look-ups take each value from the dictionary by its key. The keys are exact:household["HH_ID"]raises KeyError: 'HH_ID'.- The f-string writes the commas, the spaces and
household sizeas they are, and puts the values in the{ }. The check compares the text character by character, so a missing comma or space turns it red. - Inside an f-string written with double quotes, the keys take single quotes:
f"{household['hh_id']}". Python 3.12 and newer also accept double quotes there, but single quotes read more clearly. returnhands the text back.print(...)would show it on the screen and returnNone, which is what the check would see.
Exercise P1.2. The mean age, without the code for "not stated"
The task
Why it matters: in the farm survey, an age of 999 means not stated, not an age. A mean that includes it is wrong, with no error to warn you; module 1 shows how far off.
Input → output: a list of ages → the mean of the ages that are not 999, rounded to one decimal; None when no age is stated (all are 999, or the list is empty).
ages |
returns |
|---|---|
[58, 42, 999, 40] |
46.7: (58 + 42 + 40) / 3 |
[999, 999] |
None |
Tools: a list that starts empty, for and if to fill it, .append(value) to add to it; sum(numbers) and len(numbers); round(value, 1); if not stated: is true when the list stated is empty.
If the check is red: common causes
| what you see | the usual cause | look at |
|---|---|---|
284.8 for [58, 42, 999, 40] |
999 is counted as an age. | keep only the ages that are not 999 before the mean |
35.0 for [58, 42, 999, 40] |
The sum leaves 999 out, but the count does not: 140 is divided by 4. | len(stated), not len(ages) |
| ZeroDivisionError: division by zero | No age is stated, so the mean divides by 0. | if not stated: return None, before dividing |
46.666667 |
The mean is not rounded. | round(..., 1) |
None for every list |
return None runs every time: it is not inside the if, or the if tests the wrong list. |
the indentation of return None under if not stated: |
Still stuck after ten minutes? Ask your table, then raise your hand.
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.
The idea
Set the not-stated code aside first, then take the mean of what is left; when nothing is left, there is no mean to give, so return None.
The function, solved
Copy it with the button on the code's frame, and paste it over all the code in the exercise's cell, so the indentation stays right.
def mean_age(ages: list[int]) -> float | None: """The mean of the stated ages (999 means not stated), to one decimal; None when no age is stated.""" # Exercise P1.2: the task and examples are in the text above this cell. stated = [] for age in ages: if age != 999: stated.append(age) if not stated: return None return round(sum(stated) / len(stated), 1)Why each part is there
- The loop keeps the ages that are not
999instated. The mean must be taken over those only:999is a code, and counted as an age it pulls[58, 42, 999, 40]up to 284.8. if not stated: return Nonecomes before the division. Without it,[999, 999]and[]reachsum(stated) / len(stated), which is0 / 0, and Python raises ZeroDivisionError: division by zero.- Both the sum and the count come from
stated: dividing bylen(ages)would count the not-stated households in the mean's denominator. round(..., 1)rounds once, at the end: 140 / 3 is 46.666…, which becomes 46.7.