|
Laboratory 2: simple functions and conditional branching (if)
Prerequisits: functions, import statements, math, int, float, if-then-else, 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 re-submit 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 change Spyder’s settings to warn if the PEP8 style guide is not followed:
Preferences -> Completion and Linting -> Code style and formatting -> [ ] Enable code style linting
The Preferences entry is located under Tools on Linux and Windows, and under Python on MacOS.
Please create a new file training2.py and define the following functions in the file:
A function box_volume(a, b, c) that calculates and returns the
volume of a cuboid with edge lengths a, b, c.
Examples:
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 t (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.
You have to re-arrange the equation to obtain t(h).
Examples:
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 interprets x as a fraction between 0 and 1 that determines
how far to go towards b, starting at a. The function should return the
position described by a, b and x.
Examples:
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 v (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. You need to compute the
fall time t from a given height h.
You can re-use the fall_time function you wrote earlier
to compute t(h).
- A function signum(x) that:
- returns 1 if x > 0
- returns 0 if x = 0
- returns -1 if x < 0
Example:
In [ ]: signum(42)
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 learn@mpsd.mpg.de using training 2 as the subject line. Wait for an email reply and improve your functions (if necessary) until they pass 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:
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 the 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:
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
Use the usual email address learn@mpsd.mpg.de and lab 2 as the subject line.
Last updated: 2024-12-19 at 15:57
|