Lab 2.

Python functions

Here is our plan for today:

  • functions that return values and print results
  • functions that print an assortment of owls
  • functions to draw donuts
  • functions to draw angry birds
  • functions to create tesselation patterns

Files and Folders

We missed last week's lab (snow day), so let's recap how we get set up for lab (we'll follow these same steps for almost every lab).
  1. First, connect to the cs server cs.wellesley.edu using Fetch on a Mac (or WinSCP on a PC) and download a copy of the lab2_programs folder.
  2. Change the folder name to contain your name.
    OLD NEW
  3. After opening Canopy, set your working directory to be your (renamed) lab2_programs folder and save all your code there.

  4. There is a setting that you need to change (this is a one-time thing).
    Canopy > Preferences > Python tab. In the drop down menu for PyLab backend, select Inline(SVG) (see image below)
  5. Now you are ready to code in Python!

Task 0. Simulate rolling a die

Create a new file called die.py. Inside this file, write a function called roll that simulates rolling a six-sided die (that is, it returns an integer between 1 and 6, inclusive). You'll need to use python's random library.
roll()==> 2
roll()==> 5
roll()==> 6
roll()==> 1

Task 1. Currency conversion

Task 1a

Create a new file called converter.py. Write a function called convertEurosToUSD that takes an amount of Euros and converts it into US dollars (1 Euro = 1.15 USD, as of today's lab). Invoking your function will produce output like this:

convertEurosToUSD(19)==> 19 Euros = $21.85 US Dollars
convertEurosToUSD(100) ==> 100 Euros = $115.0 US Dollars
convertEurosToUSD(550) ==>550 Euros = $632.5 US Dollars

Note: although it did not occur with the above examples, sometimes you generate a dollar amount that extends beyond two decimal places (e.g. $10.232444). To clean this up, you can use the built-in python function round to display only 2 decimal places like this: round(25.65532,2) ==> 25.66. In case you're curious, here's a table of available built-in Python functions.

Task 1b

Write another function called convertUSDToWon that takes an amount of USD and converts it into Korean Won (KRW) (1 USD = 1092.20 KRW). Invoking your function will produce output like this:

convertUSDtoWon(20) ==> $20USD = 21844.0 KRW
convertUSDtoWon(75.39) ==> $75.39USD = 82340.958 KRW
Now you can use your functions to convert from Euros to KRW, for example 85 Euros is 106762.55 KRW.

Task 2. Happy Owls

This goal of this task is to give you more practice with functions (and to have a little bit of fun). Below are some strings that, if printed, produce an owl.
         '          '   # line A
         '     {o,o}'   # line B
         '     /)_) '   # line C
         '      " " '   # line D
         '          '   # line E
Complete the following steps. One rule that you must follow: in your entire drawOwls.py file, you may use each of the lines of code above exactly once. In other words, you may have exactly one line A, one line B, one line C, etc.
  1. Create a new file called drawOwls.py
  2. Write a function called owl() that will print an owl using the statements above.
  3. Write a second function called owlPair() that draws two owls in a column like this:
  4. Write a third function called owlQuad() that draws four owls in a column like this:
  5. Write a fourth function called owlRow(num) that draws the specified number of owls in a row like this:



Task 3. Donuts

Create a new file called donut.py. In this file, you'll write a function called donut that will create a cs1graphics donut. The picture below shows three invocations of the donut function on a 500x500 canvas.
The three invocations are as follows (after creating a 500x500 ivory canvas called plate):
blueberry_donut = donut('blue',100,200,180,'ivory')
plate.add(blueberry_donut)
chocolate_donut = donut('brown',75, 350,300,'ivory')
plate.add(chocolate_donut)
strawberry_donut = donut('pink',50,150,400,'ivory')
plate.add(strawberry_donut)
Here are details about how to write this function.
  1. The donut function has five parameters, we'll call them color, radius, x, y and backgroundcolor. Color and radius specifies the color and outer radius of the donut, respectively. x and y give the position of the donut on the canvas, and backgroundcolor is the color of the donut hole (so it matches the background color of the canvas).
  2. To make a donut, the outer circle is drawn of size radius, and then an inner circle (filled with the given backgroundcolor parameter) is drawn of size radius*0.5.
  3. It's best to create a single Layer(), and add both the inner and outer circles to your layer to make the donut. Your donut function will return the layer that contains the donut.
  4. Note: when creating graphics using cs1graphics, it is best to leave the center of the various components at the default (0,0). Then, after your graphic is fully created, move it to the desired location on the canvas.

Task 4. Angry birds

Create a new file called angryBirds.py. You'll write a function called bird that creates a single angry bird (using the cs1graphics package). The picture below shows five invocations of the bird function on a 700x500 canvas.

The five invocations are as follows (after creating a 700x500 canvas called sky):

sky.add(bird('red',60,0.5,570,200))
sky.add(bird('yellow',70,0.9,380,100)) sky.add(bird('green',100,0.2,112,275)) sky.add(bird('magenta',140,0.0,355,340)) sky.add(bird('darkolivegreen',25,0.75,205,80))

Here are details about how to write this function.
  1. The bird function has five parameters, we'll call them color, radius, anger, x and y. Color and radius specifies the color and radius of the bird, respectively. Anger is a value between 0.0 and 1.0 that controls the angle of the eyebrows. An anger value of 0.0 means very calm (as in the magenta unibrow angry bird above) whereas 0.9 is irate (as in the yellow bird above).
  2. It's a good idea to keep your bird's body with a reference point of (0,0). We will repeatedly refer to the radius of the bird's head as r (because there are many other radii, and we want this one to be distinguished from the others).
  3. The eyeball (the white part) has a radius that is 20% of r. The position of the eyeball is given in the diagram (where the intersection of the horizontal and vertical lines is at the reference point of the bird's head). The eyeball is positioned at a location that is displaced horizontally r*0.2 from center of the head, and displaced vertically a distance of r*0.1 from the center of the head. The pupil (the black dot in the eye) has a radius that is 10% of r. The pupil is positioned so that it is shifted to the right a distance that is 10% of r.
  4. The eyebrow has a width of 40% of r and a height of 7% of r. The eyebrow is positioned so that it is offset a distance of 20% of r both vertically and horizontally from both the center of the head. Keep the reference point of the eyebrow at the center of the rectangle for rotation. The amount of eyebrow rotation ranges from 0 to 60 degrees. So, an anger value of 0 corresponds to a rotation of 0, an anger value of 0.5 corresponds to a 30 degree rotation, an anger value of 1.0 means a 60 degree rotation, etc.
  5. Lastly, the x and y parameters indicate the position of the bird on the canvas.