Skip to content
Learn Python with BNHR

Start here

On this page
  1. Overview
  2. 1.1 · Say hello
  3. 1.2 · How far along is the count?

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

  • None for every name: the function prints the greeting instead of returning it. Change print(...) to return ....
  • 'Kumusta, {name}!': the f before 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.

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.0 for 1 of 3: the share is rounded before it is multiplied by 100. Multiply first, then round.
  • 0.4 instead of 37.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.

Something new

Identify results
LayerExplore a module
ModuleArrays with NumPy
AboutModule 2 of Arrays & DataFrames: arrays, their dtype and shape, masks, vectorized arithmetic and broadcasting, and sums along an axis with missing values. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CourseArrays & DataFrames, free