Lectures
Tuesday I will talk about how you can control the behaviour of your program by asking the True/False questions we talked about last week. Apart from the "if" statement, there are a few others that will make your life as a programmer much easier. I will also talk about functions which are essentially the same as those you know from math. In programming — as in math — they form a convenient way to reuse functionality and to structure your code.
In the Thursday lecture I will talk more about control flow, boolean expressions and functions.
Computer exercises
For this weeks computer exercises, complete the Control flow and functions exercise and the Files and modules exercise.
Reading material
Apart from the pages I put up I recommend chapters two, three and four from How to Think Like a Computer Scientist. These chapters have more detail than what I will describe in the lectures so think of this material as an alternative source of learning that allows you to find more detail and see the topics explained in a different way.
Weekly assignment
We will need to compute the square-root of some numbers. To do this we will use a function from the standard library. Type
from math import *
This will give you access to all the standard math functions. You can explore these by typing help(math). You will want the function called "sqrt". To get the square root of 4 type sqrt(4). Remember that to use this function in a module you will need to import the math module above the place where you attempt to use the function.
The actual assignment is a file containing the following functions:
- lengthOfHypotenuse(a, b) that finds the length of the hypotenuse in right-angled triangle, where a and b are the lengths of the two legs.
- lengthOfLeg(a, c) that finds the length of the missing leg in right-angled triangle, where a is the length of the known leg and c is the length of the hypotenuse.
- lengthOfMissingSide(x, y, area) that finds the length of the missing side in right-angled triangle, where x and y are lengths of the known two sides and area is the area of the triangle. (Ofcause x, y, and area must be values that fit a right-angled triangle.) This function should first establish if it is a leg or a hypotenuse that needs to be calculated and then call the appropriate function before returning the result.
If it works you should be able to call you functions and get these results:
>>> lengthOfHypotenuse(5, 4) 6.4031242374328485 >>> lengthOfLeg(5, 7) 4.8989794855663558 >>> lengthOfMissingSide(4, 5, 10) 6.4031242374328485
Handing in
To hand in the assignment put the code in a file named after your self and the week. If it was me it would be Kasper_Munch_week2.py. Attach it to an email with subject "Assignment" and send it to asand@cs.au.dk before 10am the following Wednesday.