Every exercise in 00_start.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 1.1. Say hello
The task
Finish greeting(name) so that it returns a greeting for that name: Kumusta, then the name, then !.
| call | returns |
|---|---|
greeting("Ana") |
"Kumusta, Ana!" |
greeting("Juan dela Cruz") |
"Kumusta, Juan dela Cruz!" |
An f-string puts a value inside a text: write f before the opening quote, and the name between braces, as in f"Hello, {name}".
If the check is red: common causes
Nonefor every name: the function prints the greeting instead of returning it. Changeprint(...)toreturn ....'Kumusta, {name}!': thefbefore the opening quote is missing, so the braces are plain text.- One character off (
'Kumusta,Ana!','Kumusta, Ana'): compare your text with the table, space and!included.
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 builds the text in one step: everything between its braces is replaced by the value of what is written there, so the name lands exactly between the two fixed parts.
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 greeting(name): return f"Kumusta, {name}!"Why each part is there
returnhands the text back to whoever calledgreeting. The check compares what comes back; it never looks at the screen, soprint(...)instead ofreturnleaves the check red withNonefor every name.- The
fbefore the quote is what makes{name}a value. Without it the result is the textKumusta, {name}!, braces and all. - The space after the comma and the
!are part of the fixed text:Kumusta,Ana!andKumusta, Anaare both one character away from right, and the check counts them as wrong.
Exercise 1.2. How far along is the count?
The task
A barangay has total households, and the enumerators have visited visited of them. Finish share_visited(visited, total) so that it returns the share visited, as a percentage rounded to one decimal place.
| call | returns |
|---|---|
share_visited(45, 120) |
37.5 |
share_visited(1, 3) |
33.3 |
share_visited(0, 80) |
0.0 |
/ divides, and round(value, 1) rounds a number to one decimal place.
If the check is red: common causes
30.0for 1 of 3: the share is rounded before it is multiplied by 100. Multiply first, then round.0.4instead of37.5: the share is not multiplied by 100.ZeroDivisionError: the inputs are the wrong way round,total / visited.
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 share is the part divided by the whole; times 100 it is a percentage. Rounding comes last, so the answer is as exact as it can be until the moment it is shown.
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 share_visited(visited, total): return round(visited / total * 100, 1)Why each part is there
visited / total * 100divides first, then multiplies./always gives a decimal number, so1 / 3is0.333..., not0.round(..., 1)keeps one decimal place:33.333...becomes33.3. Rounding the share before multiplying (round(visited / total, 1) * 100) gives30.0for 1 of 3, a different answer.- The order of the two inputs matters:
total / visitedis how many households there are per visited one, and gives a division-by-zero error before anyone has been visited.