Week 5: Functions

Week 5: Closure#

Syllabus#

  • Functions, keyword def

  • Function 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 return

  • Fruitful and void function, side effects, None type

  • Function 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#

Checkpoints as Assignments. As before, the checkpoint exercises are adapted from past exam questions. You should attempt them without help to check your understanding of the material.

Now that we have learned functions, we can formulate the tasks in the same way they will appear on exams. Starting this week, you may submit your solutions to the checkpoints under the Assignments tab in DTU Learn, as indicated by the icon. We regularly test all submitted solutions and provide feedback. If the feedback reveals mistakes, you are welcome to correct them, resubmit your work, and have your submission graded again. There are no fixed deadlines for these submissions, but do not postpone them just because there is no deadline.

Finally, your solution should contain only the code required to solve the problem as specified in the task description. Nothing more. See also What should my submission contain?.

Checkpoint 5.1: Normal Range #

The body mass index (BMI) is defined as

\[ \mathrm{BMI} = \frac{w}{h^2} \]

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:

  • height

float

The height of the person in meters.

Returns:

  • str

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

\[\begin{split} L_i = \begin{cases} 2 & \text{if } i = 0, \\ 1 & \text{if } i = 1, \\ L_{i-1} + L_{i-2} & \text{otherwise.} \end{cases} \end{split}\]

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:

  • i

int

Index of the Lucas number to find.

Returns:

  • int

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.