lab02 : Accumulator Functions and More Turtle Graphics

num ready? description assigned due
lab02 true Accumulator Functions and More Turtle Graphics Tue 02/06 08:00AM Wed 02/07 11:59PM

Lab02: Factorial Function & More Turtle Graphics


Goals for this lab

By the time you have completed this lab, you should be able to:

  1. Write functions that use what the textbook calls the "Accumulator Pattern"
  2. Know more about the manipulation of Python objects, as demonstrated with the use of the Turtle graphics module.

Step by Step Instructions

Step 0: Get together with your assigned lab partner. THIS LAB IS A REQUIRED PAIR PROGRAMMING ASSIGNMENT. LAB PARTNERS MUST BE IN THE SAME LAB. NO EXCEPTIONS.

Step 1: Log in, and create a lab02 directory Decide which one of you will be the first pilot for this lab, and then log in to the pilot’s account and create a directory called lab02 inside his/her cs8 folder. You will work in this account for the rest of the lab, but remember to switch often between pilot and navigator roles during the course of the lab.

From inside your lab02 directory, copy a file I’ve made for you over with the following command:

cp ~zmatni/public_html/cs8w18/labs/lab02/lab02.py ./lab02.py

Step 2: Bring up IDLE as usual, and open a new window for function definitions Start IDLE. Then, select "File->Open" and open the lab02.py file. Put your names and today’s date in the appropriate comment and save your file as lab02.py inside your lab02 directory.

Note that line 9 has the following comment:

# IMPORTANT: Students: do not change any part of the code below

IMPORTANT: MAKE SURE THAT YOU DO NOT MAKE ANY CHANGES TO THE CODE BELOW THAT LINE!!!

Step 3: Practice writing a function to accumulate products

You will write a function called factorial(n) that computes the product of the first N numbers, where N is a parameter. For example, factorial(4) is calculated as 1 * 2 * 3 * 4, so the function should return the value 24. We will assume N is greater than 0. I did a similar exercise in lecture.

Be sure to understand the purpose and meaning of each of the following instructions before you execute them.

  1. Skip a blank line (in your lab02.py file) under the first 4 lines, and then type a brief comment for a factorial function such as the following:
      # factorial - returns n factorial 
      # assumes n is greater than or equal to 0
      
  2. On the very next line, type the function header as follows:
    def factorial(n):
  3. Now write the rest of this function. It has to return the factorial value of n. Make sure that the function checks on the fact that n is a positive, non-zero number.

Save and run the module. Then test the function a few times (in the Python shell window) by comparing its results to ones that you calculate manually. Here are 2 example runs of our solution:

>>> factorial(3)
6
>>> factorial(5)
120

Step 4. Getting Started with pat, the (perfectly adorable) turtle

First make sure that you can show turtle graphics. Try this command at the Python shell prompt (>>>):

>>> import turtle
>>>

If what you get back is just another Python prompt (not a bunch of error messages) then you are good to go.

Then type (still in the Python shell window) the following command:

>>> pat = turtle.Turtle("turtle")

That last command will open another new window - probably titled “Python Turtle Graphics” - and eventually this window will display your drawings. But ignore it for now. In fact, you will need to recreate it later.

Back in the new file window (lab02.py file), type the drawRectangle function that we did together in class and add a comment in front of it, as in the example below:

# function to draw a rectangle
# parameters: name of a turtle, and the width and height to draw
# draws: a rectangle at the position of the turtle
  def drawRectangle(turtleObject, width, height):
# This is where your code goes...

Once you have typed in the function, save the file, and use the Run=>Run Module command to "compile" your Python code. You might get an error message when you do this the first time - read the message, and figure out what to fix in your function definition. Keep fixing, saving and running the module until it works without error messages. Then look in the Python Shell window - you should see something like this:

>>> ================================ RESTART ================================
>>>

Congratulations! You just compiled and loaded into memory a useful function that we can use to make drawings.

Unfortunately though, IDLE just destroyed a portion of the work you did earlier: pat the turtle is gone from memory (the Python shell was restarted). Not such a big loss this time - just two commands to repeat, but annoying nonetheless. Thankfully IDLE does provide a way to repeat prior commands. Try that way now: click (with the mouse) on the “import turtle” line (inside the Python Shell window) and then hit the enter key - notice the command appears at the >>> prompt - hit enter one more time to, well, enter it. Repeat the “pat = …” command too.

Now, to test your function, try calling it with various values for width and height:

>>> drawRectangle(pat, 20, 50)
>>> drawRectangle(pat, 100, 75)

You can also try moving pat between the rectangles, picking up the pen first—for example this will move pat over a bit before drawing the next rectangle.

>>> pat.up()
>>> pat.forward(100)
>>> pat.down()
>>> drawRectangle(pat, 60, 80)

Copy the drawRectangle function into the lab02.py file just below the last function you wrote in there.

After playing with pat and the drawRectangle function for awhile, move on to Step 5.

Step 5: Write a function to draw a house

First switch roles between pilot and navigator if you did not already do that.

Add a function named drawHouse() to the lab02.py file according to the following specifications:

  1. Add a couple of blank lines after the end of your drawRectangle function definition.
  2. The new function header must name two parameters in this new function: a turtle, and a width.
  3. Precede the header with an appropriate comment like you did for drawRectangle.
  4. The function body should use the turtle parameter to draw a house - make the height of the house proportional to its width. Use the drawRectangle function to make part of the house - abstraction is good!

It should work correctly if used as follows:

>>> drawHouse(pat, 100)    # a really big house
>>> drawHouse(pat, 10)     # a tiny house (just 10 pixels wide)
Here is an example:House Note: your house does not have to look exactly like this—i.e. the proportions of the width/height and the steepness of the roof can be different. As long as it "looks like a house", that's good enough for this lab.

Copy the drawHouse function into the lab02.py file just below the last function you wrote in there.

Step 6: Draw a block of houses

  • Make a function to draw a block (row) of houses. Precede the function definition with an appropriate comment, like always! Write the function header to take three parameters: a turtle, the width of one house, and the number of houses to draw:
    def drawBlock(turtle, width, number):

    The function should draw as many houses as the parameter named number specifies, and each house should have the specified width. Separate the houses by moving the turtle without drawing any lines - pick up the pen, move, and then put it down again (how do you do that? Look at the hints in Step 4). You should separate the houses by 1/2 house width. Here is an example (12 houses, each width 25 in the original drawing):

    row of houses

Copy the drawBlock function into the lab02.py file just below the last function you wrote in there.

Step 7: Show off your work and get credit for the lab

Get your TA's attention to inspect your work, and to record your lab completion.

Step 8: Submit your work to submit.cs

Your final submission, the file called lab02.py, should now have 4 functions defined therein: one for the factorial function and the other 3 for the Turtle graphics functions, drawRectangle, drawHouse, and drawBlock. MAKE SURE ALL FOUR FUNCTIONS ARE COPIED INSIDE YOUR lab02.py FILE!!

Navigate to the submit.cs website, select lab02, register your partner, and then upload your lab02.py file. EDIT: You will see 80/80 points, if it passes the tests.

You will earn the other 20 points (to make this lab worth a total of 100 points) as follows:

1) Your TA will manually have to look at your 3 drawing functions and see that they work. This is worth 10 points. If you do not finish this part of the lab during regular hours, you will not get this 10 point credit.

2) Your TA will also manually look at your submitted code on submit.cs and verify that you did the correct work. This is worth another 10 points.


Copyright 2018, Ziad Matni, CS Dept, UC Santa Barbara. Adapted from work by Phill Conrad and others. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved