Every exercise in 10_values.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 2.1. Population density
The task
Finish density(population, area_km2) so that it returns the number of people per square kilometre, rounded to a whole number. round(value) with no second input rounds to a whole number, and gives back an int.
| call | returns |
|---|---|
density(1401, 0.12) |
11675 |
density(880, 0.4) |
2200 |
If the check is red: common causes
- A decimal number such as
11675.000000000002: the result is not rounded. Returnround(...)of it. - One less than expected:
int(...)cuts the decimals off;round(...)rounds to the nearest. - A tiny number: the division is the wrong way round, area divided by population.
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
Density is people per unit of area: the population divided by the area. Rounding to a whole number comes last, because a person per square kilometre is the smallest unit worth reporting.
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 density(population, area_km2): return round(population / area_km2)Why each part is there
population / area_km2gives afloatsuch as11675.000000000002: decimal numbers are stored in binary, so a few tiny digits can appear at the end.round(...)with one input rounds to the nearest whole number and gives back anint, which also clears those stray digits.int(...)instead ofround(...)cuts the decimals off instead of rounding:2199.9becomes2199, one person short.
Exercise 2.2. A barangay's code as text
The task
Barangay codes are three digits, with leading zeros: barangay 7 is "007". Finish barangay_label(name, number) so that it returns the name, a space, and the three-digit code in brackets.
| call | returns |
|---|---|
barangay_label("Malanday", 7) |
"Malanday (007)" |
barangay_label("Barangay 143", 143) |
"Barangay 143 (143)" |
If the check is red: common causes
AttributeError: 'int' object has no attribute 'zfill':.zfillworks on text. Turn the number into text withstr(...)first."Malanday (7)": the code is not padded. Use.zfill(3).- A missing space or bracket: compare your text with the table, character by character.
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
The code is text, so the number becomes text first; .zfill(3) then pads it on the left with zeros to three characters, and an f-string puts the pieces together.
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 barangay_label(name, number): code = str(number).zfill(3) return f"{name} ({code})"Why each part is there
str(number)turns7into"7"..zfillbelongs to text, not to numbers, sonumber.zfill(3)raises anAttributeError..zfill(3)adds zeros only until the text is three characters long:"7"becomes"007","143"stays"143".- The f-string writes the brackets and the space as they are, and puts the name and the code between them.
Exercise 2.3. Is it a large household?
The task
The barangay counts a household as large when it has six members or more. Finish is_large(members) so that it returns True for a large household and False otherwise. A comparison already gives True or False, so no if is needed.
| call | returns |
|---|---|
is_large(6) |
True |
is_large(3) |
False |
If the check is red: common causes
- Wrong for exactly 6:
>leaves six out. "Six or more" is>= 6. "True"with quotes: that is text, not the valueTrue. Return the comparison itself.
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
A comparison is itself a value, True or False, so the function can return it directly.
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 is_large(members): return members >= 6Why each part is there
members >= 6isTruefor 6 and more, andFalsebelow: "six or more" includes six, so it is>=, not>.if members >= 6: return True else: return Falsegives the same results in four lines; returning the comparison says the same thing in one.
Exercise 2.4. A line for the report
The task
Finish report_line(barangay, population, share) so that it returns a line such as Barangay 143: 1,401 people (0.5%): the population with a thousands separator, and the share (already a percentage) with one decimal place.
| call | returns |
|---|---|
report_line("Barangay 143", 1401, 0.4567) |
"Barangay 143: 1,401 people (0.5%)" |
report_line("Tumana", 25000, 12.0) |
"Tumana: 25,000 people (12.0%)" |
If the check is red: common causes
1401without a comma: the population needs the:,format.0.4567%: the share needs:.1f.
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
An f-string can format each value as it puts it in: after the name, a colon and a format code. , adds thousands separators, .1f shows one decimal place.
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 report_line(barangay, population, share): return f"{barangay}: {population:,} people ({share:.1f}%)"Why each part is there
{population:,}writes1401as1,401and25000as25,000, without changing the number itself.{share:.1f}rounds the share for display:0.4567shows as0.5,12.0as12.0, so every line has the same shape.- The
%sign after the share is plain text; in an f-string it needs nothing special.
Exercise 2.5. Fix the function
The task
The function below should return a head of household's age next year, but it raises an error. Run its Try it cell, read the last line of the message, and fix the function.
| call | returns |
|---|---|
age_next_year(47) |
48 |
If the check is red: common causes
TypeError: the function still adds the text"1". Remove the quotes.'471': the age was turned into text and"1"joined to it. Add the number1to the number instead.
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
The error says + was given a number and a text. The age is a number, so the one to add must be the number 1, not the text "1".
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 age_next_year(age): return age + 1Why each part is there
"1"in quotes is text.age + "1"asks Python to add a number and a text, which it refuses with aTypeError.age + 1adds two numbers. The fix is to remove the quotes; turning the age into text (str(age) + "1") would give"471", a text joined, not a number added.