
Laboratory 2: simple functions and conditional branching (if)
Prerequisits: functions, import statements, math, int, float, ifthenelse, PEP8
In Laboratory 1 you have familiarized yourself with the submission and testing system. This laboratory consists of two parts:
 a training component (where you work on a file called training2.py) and a
 an assessed component (where you work on a file called lab2.py).
For the training component you can resubmit your work as often as you like to get it tested.
For the assessed component, you can only submit your file once: the
first email that reaches the submission email account with both a
subject lab 2 and an attachment lab2.py will be used to determine your mark for that lab.
In this lab, a fraction of the mark is given for code style that is
PEP8 compliant. To help you with this, we recommend to
Please define the following functions in the file training2.py:
A function box_volume(a, b, c) that calculates and returns the
volume of a cuboid with edge lengths a, b, c.
Examples:
>>> print(box_volume(1, 1, 1))
1
>>> print(box_volume(1, 2, 3.5))
7.0
>>> print(box_volume(1, 1, 0))
0
Examples (IPYthon):
In [ ]: box_volume(1, 1, 1)
Out[ ]: 1
In [ ]: box_volume(1, 2, 3.5)
Out[ ]: 7.0
In [ ]: box_volume(1, 1, 0)
Out[ ]: 0
A function fall_time(h) that returns the time (in seconds) needed for an
object falling from a tower of height h (in meters) to hit the ground
(ignoring air friction). Use
with h being the fall distance after time t.
Examples:
>>> print(fall_time(10))
1.4278431229270645
>>> print(fall_time(1))
0.4515236409857309
Examples (IPython):
In [ ]: fall_time(10)
Out[ ]: 1.4278431229270645
In [ ]: fall_time(1)
Out[ ]: 0.4515236409857309
A function interval_point(a,b,x) that takes three numbers and
interprets a and b as the start and end point of an
interval, and x as a fraction between 0 and 1 that determines
how far to go towards b, starting at a.
Examples:
>>> print(interval_point(100, 200, 0.5))
150.0
>>> print(interval_point(100, 200, 0.2))
120.0
Examples (IPython):
In [ ]: interval_point(100, 200, 0.5)
Out[ ]: 150.0
In [ ]: interval_point(100, 200, 0.2)
Out[ ]: 120.0
A function impact_velocity(h) that returns the velocity (in
metre per second) with which an object falling from a height of
h meters will hit the ground. Use v(t)=g*t, with v(t) the
velocity at time t, and g=9.81 m/s^2.
 A function signum(x) that:
 returns 1 if x > 0
 returns 0 if x = 0
 returns 1 if x < 0
Example:
>>> print(signum(2012))
1
Example (IPython):
In [ ]: signum(2012)
Out[ ]: 1
Remember to carefully check each function for correct implementation (see Testing tips).
Now submit your training2.py file (repeatedly if necessary) by emailing it to python4computing@gmail.com using training 2 as the subject line. Wait for an email reply and improve your functions (if necessary) until it passes all the
tests. Seek help of demonstrators to interpret the error messages if
necessary  it is important that you learn how to read those.
Create a file lab2.py that contains the following functions:
A function seconds2days(n) which accepts a number of seconds
(either as int or float) and converts the number of seconds in the
corresponding number of days. The function should return the number
of days as a floating point variable.
Example:
>>> seconds2days(43200)
0.5
Example (IPython):
In [ ]: seconds2days(43200)
Out[ ]: 0.5
For your entertainment: how many days are 10! seconds? (10!=3628800).
A function box_surface(a,b,c) that computes and returns the
surface area of a box (i.e. cuboid) with these edge lengths a, b, and c.
Imagine we need to paint the surface of the box and thus need to know its area to buy the right amount of paint.
Example:
>>> print(box_surface(1, 1, 1))
6
>>> print(box_surface(2, 2, 0))
8
Example (IPython):
In [ ]: box_surface(1, 1, 1)
Out[ ]: 6
In [ ]: box_surface(2, 2, 0)
Out[ ]: 8
A function triangle_area(a,b,c) that computes and returns the area A of a triangle with edge lengths a, b, and c. You can use the equation
Remember to carefully check the functions for correctness before you submit the file lab2.py: Your marks for this lab will be based on the outcome of the test of that submission. Use the usual email address python4computing@gmail.com and lab 2 as the subject line.
PS: While you can submit lab 2 repeatedly to get further feedback on improved versions of your code, it is your first submission that will be used for assessing your work for this laboratory session.
Last updated: 20161030 at 12:17
