lab02 : Lab02 - Using the Turtle Module and Writing Simple Functions

num ready? description assigned due
lab02 true Lab02 - Using the Turtle Module and Writing Simple Functions Wed 04/19 08:00AM Fri 04/21 11:59PM

Lab02: Using the Turtle Module and Writing Simple Functions in Python


Goals for this lab

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

Step by Step Instructions

Step 0: Get together with the lab partner you were assigned last week

If your partner is not present at the start of lab, please wait for five minutes and then ask the TA to pair you with someone else for this week.

Step 1: Be sure you are using a system that can show turtle graphics

For the rest of these instructions, we are going to assume you are working in Phelps 3525 or CSIL. If not, you will need to adapt the instructions to your computing environment.

Step 2: Log in, create a lab02 directory, and open IDLE

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 roles a couple of times between pilot and navigator during the course of the lab.

By the way, there are two ways to create a new directory (folder):

Step 3: Open a "New Window" for function definitions

Next, in IDLE, select "File=>New Window" to open a new "untitled" window for Python code (just like you did in lab00 for zen.txt).

When it comes up, click and drag the window by its title bar over to the right of your Python Shell window (so the shell window is not covered up).

Once you have opened that file, add a comment to the top of your (Untitled) file like this one, substituting your own name(s) and the current date in the proper spots:

# lab02.py
# Name: Agnes Nitt and Jason Ogg, 1/20/2015
# some functions to draw pictures using Turtle Graphics

Then, save it under the name lab02.py inside your lab02 directory.

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

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)

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: 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.

Step 6: 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.

Don’t leave early though … see challenge problems below.

Step 6. Turning in your lab.

You MUST have both your name and your partner’s name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525. Use the following command after you cd into your cs8/lab02 directory:

turnin Lab02@cs8 lab02.py

After answering the questions, be sure to wait for the message indicating success.


Evaluation and Grading

Each student must accomplish the following to earn full credit for this lab:


Optional Extra Challenge

  • 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. We separate the houses in our solution by 1/2 house width. Here is an example (12 houses, each width 25 in the original drawing):

    row of houses

  • Add a door and window(s) to your house, by modifying your drawHouse function. Of course you should use drawRectangle to help. Maybe add a chimney, eaves on the roof (by adjusting to let it overhang the sides), and/or other interesting features. After you test the new drawHouse function, and if you used it in your drawBlock function, then try drawBlock again to see your new features in a whole row of houses.
  • It should not be too difficult for you now to create a picketFence function - each slat of the fence is like a tall, skinny (plain) house, and a section of these slats is like a row of houses.
  • Still have time after writing picketFence? Then why not try a drawYard function that draws a house with a picket fence in front of it? You might even try to add a driveway, and a garage, and ...

Template & copy; 2009, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved. Adapted by Diana Franklin, Michael Costanzo, and Ziad Matni.