
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 height in metres. A person is considered to have a normal weight if their BMI is in the range from \(18.5\frac{kg}{m^{2}}\) to \(25\frac{kg}{m^{2}}\), including both endpoints.
Write a function that takes a height in metres as input. The function should return a string in the format Normal weight range: X to Y kg, where X and Y are the smallest and largest whole-number weights that give a BMI in the normal range.
Consider the height \(1.73\). The weight limits for the normal range can be calculated as \(w_\mathrm{lower} = 18.5 h^2=55.36865\) and \(w_\mathrm{upper} = 25 h^2= 74.8225\). To ensure that weight falls into the normal range, the lower limit should be rounded up and the upper limit should be rounded down. When rounded, the normal weight range should be as shown below.
>>> normal_range(1.73)
'Normal weight range: 56 to 74 kg'
The filename and requirements are:
normal_range.py
normal_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.py
lucas_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.