
Warning
This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.
Week 5: Closure#
Syllabus#
Functions, keyword
defFunction names and naming convention
Function parameters (names given by the function definition)
Function arguments (values passed to the function when called)
Function body and indentation
Calling functions
Variable scope
Returning values, keyword
returnFruitful and void function, side effects,
NonetypeFunction examples: build-in functions, functions included in the standard Python library, functions from common third-party libraries, user-defined functions.
Tracebacks in error messages
Good practice when writing functions (start by scripting, incremental development, scaffolding)
Testing functions
Writing tests for functions
Documenting functions
Checkpoints#
Checkpoint 5.1: Normal Range #
The body mass index (BMI) is defined as
where \(w\) is body weight (mass) in kilograms, and \(h\) is the height in metres. A person is categorized as being of normal weight if their BMI falls in the range from \(18.5\frac{kg}{m^{2}}\) to \(25\frac{kg}{m^{2}}\), both endpoints included.
Write a function that takes a height in meters as input. The function should return a string Normal weight range: X to Y kg with the normal weight range, where X and Y are the smallest and the largest weight in whole kilograms, which when converted to BMI fall into normal range, respectively.
Consider the height \(1.73\). The weight limits for the normal range can be calculated as \(w_{lower} = 18.5 \cdot h^2\) and \(w_{upper} = 25 \cdot h^2\). The weight should be expressed in whole kilograms, so both limits should be rounded. To ensure that weight falls into the normal range, the lower limit should be rounded up and the upper limit should be rounded down.
You can see the desired behavior below.
>>> normal_range(1.73)
'Normal weight range: 56 to 74 kg'
The filename and requirements are:
normal_range.pynormal_range(height)
Calculates the normal weight range based on height.
Parameters:
|
|
The height of the person in meters. |
Returns:
|
A string indicating the normal weight range in kilograms. |
Use the following script to check your function test_normal_range.py. If your function fails the test in this script, it will also fail when you hand it in.
Checkpoint 5.2: Lucas Number #
The Lucas sequence is an integer sequence similar to the Fibonacci series. Every Lucas number is a sum of the previous two Lucas numbers, given that the first two Lucas numbers are 2 and 1. That is, Lucas numbers are defined by
The start of the Lucas sequence is 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, …
Write a function that, given an index \(i\), returns the Lucas number with that index, that is \(L_i\).
For example, if the input is \(i = 3\) the function should return \(L_3\). We can start by using \(L_1\) and \(L_0\) to form \(L_2 = L_1 + L_0 = 3\). Similarly we continue with \(L_2\) and \(L_1\) to compute \(L_3 = L_2 + L_1 = 3 + 1 = 4\). Therefore the function should return \(4\).
The desired behavior is shown below.
>>> lucas_number(3)
4
The filename and requirements are:
lucas_number.pylucas_number(i)
Finds the Lucas number at the given index.
Parameters:
|
|
Index of the Lucas number to find. |
Returns:
|
The Lucas number at the given index. |
Use the following script to check your function test_lucas_number.py. If your function fails the test in this script, it will also fail when you hand it in.