CS8, Spring 2017

Lab02:
Working with Pat the Turtle
Writing simple functions


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'll 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've 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, 4/18/2017
# 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 you were asked to write in Hw2 (if you did not do it then, you will have to do it now - instructions are in Hw2), 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've 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

IMPORTANT: You must try this step, but you don't have to succeed at it to earn full credit. Please begin it and try to finish it, but if you are running out of time before lab ends, then move on to Step 6 - the TA will check off your work even if it is not exactly correct, as long as you can show evidence that you tried.

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 doesn't 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 6a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

In the unlikely event that you must complete this assignment at CSIL, then submit it with the turnin program - but do not turn it in if the TA already checked you off. 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@cs8c 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


Template © 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 and Michael Costanzo.