Skip to content
Learn Python with BNHR

Values and names

On this page
  1. Overview
  2. 2.1 · Population density
  3. 2.2 · A barangay's code as text
  4. 2.3 · Is it a large household?
  5. 2.4 · A line for the report
  6. 2.5 · Fix the function

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. Return round(...) 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.

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': .zfill works on text. Turn the number into text with str(...) 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.

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 value True. 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.

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

  • 1401 without 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.

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 number 1 to 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.

Something new

Identify results
LayerExplore a debugging drill
Debugging drill D2Debugging drill: the report that goes wrong
AboutThe second debugging drill of Arrays & DataFrames, after module 5: five mistakes that put wrong numbers in a survey report, to predict, trace, fix and explain.
CourseArrays & DataFrames, free