Week 5: Functions

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 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, re-submit your work, and have your submission graded again. There are no fixed deadlines for these submissions, but don’t postpone them just because there is no deadline.

Checkpoint 5.1: Normal Range #

The body mass index (BMI) is defined as

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

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