Munch Lab

Applied programming 2014 week two


This week I will talk about what modules are, what functions and how you use them, how you can condition the execution of statements. I will also talk some more about iteration and introduce you to the string datatype and all the things you can do with string.

Computer exercises

You will be solving the exercises in the chapters “Python modules” and “Functions”. This will train your ability to spot repetitiveness when solving problems and give you some practical experience with using modules and functions. We will end with a the Turtle Challenge: who can make the coolest turtle graphic with lines only. I will try to make them all available to you so you can vote for who should win.

Reading material

The chapters: Python modules, Functions, Selection, More about iteration, and Strings from  (our version of) How to think like a computer scientist.

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

import math

This will give you access to all the standard math functions. You can explore these by typing help(math). You will want the function “math.sqrt()”. To get the square root of 4 type math.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. You are not supposed to just use the math.hypot() function!
  • 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.

If it works you should be able to call you functions and get these results:

lengthOfHypotenuse(5, 4)


lengthOfLeg(5, 7)


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 Attach it to an email with subject “Assignment” and send it to Dan ( You can see your hand in deadline on the main course page.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: