CS8, Fall 2010

lab01: ("lab one")
Working with Fred the Turtle:
writing simple functions


Goals for this lab

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

This is a pair programming lab!

Work with your assigned pair programming partner on this lab. If you do not have one, consult the TA for your lab and/or your instructor for advice prior to starting.

Watch the pair programming video from NC State to prepare

You can find the video "Fun with Pair Programming" at this link. It takes less than 10 minutes to watch it.

How pair programming will work

What you'll do

Just as the in the video "Fun with Pair Programming", you and one other person will sit at a single terminal and write code.

From the video, you should be familiar with the roles of "driver" and "navigator", and the need for switching roles often. If not, then watch the video.

How you'll be matched with a pair

How your pair programming will be evaluated

You'll be asked to evaluate your pair partner, and your pair partner will also evaluate you. These are the questions you'll be asked. You may find it helpful to consider those before you begin work—you certainly want your pair partner to say good things about you.

(Previous experience with pair-programming in courses suggests that 95% of students do end up giving their pair good evaluations—make sure you are part of the 95%!)

1. Did your partner read the lab assignment and preparatory materials before coming to the scheduled lab? [For example, did he/she watch the pair programming video before coming to lab]
2. Did your partner do their fair share of the work?
3. Did your partner cooperatively follow the pair programming model
(rotating roles of driver and navigator)?
4. Did your partner make contributions to the completion of the lab assignment?
5. Did your partner cooperate?

Your pair partners evaluation will be counted as part of your grade for the course.

So please do work with your pair in a constructive way!

Also: know that the vast majority of your grade in this course is still based on individual work, not pair work—so while you do need to try to have the best pair experience possible, in the unlikely event that your experience does not go well, the impact on your grade is limited.

The pair programming roles: driver (or driver) and navigator

This may be your first experience with "Pair Programming." You will learn more about it as the quarter progresses. There are two roles: driver and navigator.

Step by Step Instructions

Step 0: If you haven't completed and turned in lab00, do so now

The deadline for lab00 is Friday 10/01 at 5pm and ideally you completed it before that time. (The deadline for lab01 is also 10/01 at 5pm.)

But if you haven't yet completed it, do it now even if it is late. I will accept lab00 without penalty through Friday 10/01 at 5pm.

After that, you cannot earn any points for lab00, but you are still responsible for completing it before we will give you a non-zero grade on any other lab.

Step 1: Work at a computer where you can use Turtle Graphics

That means, either

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

If you are working on a PC or Mac, you may need to consult the page below for help with getting cTurtle.py correctly loaded onto your system before starting this lab. If you are having trouble, you may ask for instructor if there are resources to help you with this.

Finally, if you work on your own PC or Mac, you are STILL RESPONSIBLE FOR UPLOADING YOUR FILES to the csil.cs.ucsb.edu machines so that you can turn them in using the turnin program. You can use a file transfer program such as WinSCP on Windows, or on Mac CyberDuck or Fetch. Figuring that out is up to you—but you may use the General Forum on Gauchospace to share information with one another about how to accomplish this on Windows or mac.

There is more information below in step 9a

Step 2: Register your pair on Gauchospace

On Gauchospace, there is a "forum" (i.e. a discussion board) where you can register that you are a pair working together on lab01. Please log on to http://gauchospace.ucsb.edu with your UCSBNetID—your username is your UMail address without the @umail.ucsb.edu part, and your password is the one you use for umail. Then, find the "Forum for lab01 pair partners", and follow the instructions there to make a post registering your pair.

This step is almost 10% of your grade for this lab (20 pts out of 220), so be sure you do it!

Step 3: Create a directory for lab01

First, create a directory (folder) called lab01, inside your cs8 folder, which is in turn, inside your home directory.

There are two ways to do it:

If you've forgotten how, consult the instructions for lab00. If you still can't figure it out, you may ask a classmate, or the TA or instructor for help.

Step 4: Bring up IDLE

To bring up IDLE, type this command in the Terminal window, just like we did last week:

	~pconrad/cs8/cs8idle

When you do, you should see the IDLE program come up. The window that appears should have the Python Command prompt (>>>) in it.

 

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

Next, in IDLE, select "File=>New Window" to open a new "untitled" window for Python code.

When it comes up, click and drag the window by its title bar over to the right of your Python Shell window.

Here's what that looks like:

Selecting File=>New Window The "untitled" window appearing Dragging the windows so they are side-by-side
Selecting File => New Window in IDLE New window appears New windows side by side

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

# lab01AgnesNitt.py by Agnes Nitt, for CS8 lab01,  08/12/2010
# some functions to draw pictures using Turtle Graphics

Then, save it under the name lab01YourName.py—for example:

Now proceed with Step 6.

Step 6. Getting Started with Fred the Turtle

Now, you should be ready to get started with Fred the Turtle.

First, to make sure that you have Turtle Graphics available, try this command at the Python shell prompt (>>>):

>>> import cTurtle
>>>

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

Next, in the right hand window (the one with the function definitions), put the following commands, after the initial comments:

import cTurtle
fred = cTurtle.Turtle("turtle")

Next type in the function definition for drawing a square that we discussed in lecture. You'll put this in the window with the function definitions. If you've already got this function in your file, be sure you add a comment in front of it, as in the example below:

# function to draw a square
# consumes: name of a turtle, and length of a side
# draws: a square, starting in the direction the turtle is facing,
# and drawing the top of the square first

def drawSquare(t, sideLength):
for i in range(4): t.forward(sideLength) t.right(90)

Once you've typed this in, use the Run => Run Module command to "compile" your Python code. In the Python Shell window, you should see something like this:

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


Now, to test your function, try calling it with various values for sideLength:

>>> drawSquare(fred, 200)
>>> drawSquare(fred, 50)

You can also try moving fred between the squares, picking up the pen first—for example this will move Fred over a bit before drawing the next square. The semicolons just allow us to put lots of function calls all on the same line.

>>> fred.right(90); fred.up(); fred.forward(200); fred.left(90); fred.down()
>>> drawSquare(fred, 75)

You should see that you can draw squares of various sizes.

If that's true, then move on to Step 7.

 

Step 7: Make your own functions

Next, write your own functions to do each of the following. As you make the functions, test them.

 

(a) Draw a Triangle (any kind will do—could be equilateral, or could be something else.)

(b) Draw a Yield sign like this one. (Here, the sign should be an equilateral triangle)

A yield sign

(If you haven't switched roles yet (driver/navigator), in between steps b and c is a good time to do it.)

(c) Draw a House similar to this one

(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.)

House

 

(d) Come up with your own drawing. Try to make it something that can be generalized—that is, have one or more parameters to the function that makes your drawing.

The parameter might be: the length, height, width, or the x and y location of where you "goto" before you start the drawing. You may like to think about whether you include a "penup" or "pendown" in your function to be sure the turtle actually draws something.

(Switch roles at least once during making your own drawing.)

A frequently asked question is: how complicated does "our own drawing" need to be?

The answer is: not very complicated. We just want to see that you have the basic idea of how this works. There will be time for more complicated drawings later in the course. So for today, keep it simple.

Step 8: Make a driver function called go()

Next, to help your TA test your work, you are going to make a driver function called go().

The driver function is one that will make several function calls to your functions to show that they work properly. You may want to move the turtle around the screen a bit to make room for all of your drawings. As you do, you can pick up the pen and then put it down again.

Here is an example of a driver function, and the resulting drawing. Your driver function does not have to be exactly like this one—in fact, I encourage you to experiment and change it up a bit. The only requirement is that you need to call each of your other functions at least once, and the drawings should not overlap each other.

You'll see that after the definition of the driver function there is this line, all by itself

go()

That line is a function call to the driver function, and it is the last line in the file—that way, each time you choose Run => Run Module or hit F5, the functions are all defined, and then the go() function is automatically called. This way, if your TA loads your program and hits F5, the TA can immediately see if your program works properly.

Example driver function (and call to go() ):

# Make a driver function

def go():
   
   # back up Fred 200 pixels

   fred.up(); fred.backward(200); fred.down()

  
   # draw a square, and move over a bit

   drawSquare(fred, 50);
   fred.up(); fred.forward(75); fred.down()

 
   # draw a triangle, and move over a bit

   drawTriangle(fred, 60)
   fred.up(); fred.forward(75); fred.down()

 
   # draw a yield sign, and move over a bit

   drawYieldSign(fred, 50)
   fred.up(); fred.forward(100); fred.down()

 
   # draw a house sign, and move over a bit

   drawHouse(fred, 100)
   fred.up(); fred.forward(150); fred.down()

 
   # draw a hexagon, and move over a bit

   drawHexagonThing(fred, 40)
   fred.up(); fred.forward(110); fred.down()


   # draw a bunch of hexagons
   
   for i in range(40,70,10):
      drawHexagonThing(fred, i)
 

go()

Sample output (yours may look different—in fact, I hope that it will!)

whole picture

When you have a driver function you are almost ready for submission. Just a few minor checks first

 

Step 9: Check over your program to see if it is ready to submit

Before you submit your assignment, check these things:

Step 9a: (only needed if you were working on a PC or Mac) Transferring your file

There are several ways to get the file lab01YourName.py from your PC or Mac onto CSIL.

One of the easiest, though not the most efficient or direct is via email:

There are better ways though—ways that do not require you to visit Cooper or CSIL in person. The most straightforward way is this:

Because there are so many possible options, describing them all in detail is not feasible in this space. Your TA or instructor may be able to provide guidance on how to learn about file transfer options that will work for your situation—but keep in mind that figuring this out for your specific PC and operating system is your responsibility, not ours. We provide CSIL as an alternative for all students—and during the regular school year, CSIL is available 7 days a week, from early in the morning until late at night.

Step 10: Submit your assignment using the turnin program on CSIL

To submit your assignment, you need to bring up a terminal window on CSIL.

Next, we use the cd command that we practiced earlier:

-bash-3.00$ cd 
-bash-3.00$ pwd 
/cs/student/yourusername 

-bash-3.00$ cd cs8 
-bash-3.00$ pwd 
/cs/student/yourusername/cs8 
-bash-3.00$ 

When you are in inside your cs8 directory, you are ready for the turnin step.

Type the following at the prompt:

turnin lab01@cs8 lab01

You should be asked if you want to turn in this program. Respond "yes", and then you should get a message indicating that your efforts were successful!


Evaluation and Grading (220 pts)

Due Date: Friday 10/01, 5pm Wednesday 10/06, 11:59pm

But: you are encouraged to finish it sooner.

If you need to share files

Before you finish up if you didn't submit yet, you'll copy your work to each of the other student's accounts on your team.

Note: DO NOT share passwords with each other.

That is a violation of your account agreement, and can result in suspension of your computing privileges at UCSB.

Here's what you can do instead:

OR use the scp solution below, which is even better.

That way, if your pair partner gets hit by a bus, you can continue working without him/her—you aren't 'out of luck'.

A better solution is to use scp to copy the work from person1's account into person2's account with the scp command—here's how:

  1. , have person2 log into their CSIL account.
  2. Person2 cd's into their ~/cs8 directory
  3. Person2 types this command
    scp -r person1@csil.cs.ucsb.edu:cs8/lab02 lab02.snapshot.date.time
    • replace person1 with person1's CSIL username (e.g. jsmith)
    • replace date.time with the date and time (e.g. Oct29.1pm)
    • don't include any slashes or spaces in the date.time string—it must be a valid part of a filename.
  4. After you type this command, you'll be prompted for a password. Person1 should type his/her password. Don't share the password, just have person1 type it in person2's window.'
  5. If it works, there will now be a directory called lab02.snapshot.date.time in person2's directory that is a complete copy of the ~/cs8/lab02 directory from person1's account.
    • Please read the "final thoughts" below so that you understand what the proper use of this process it—it is a powerful command, but it has the potential to be abused. With great power comes great responsibility.

It is only necessary for one of you to submit the work via turnin, as long as both names are in each file.

But, if you are the person whose account is not the one from which the turnin step is done, be SURE that your name appears! You need to work carefully with your pair partner to avoid misunderstandings and problems.

Some final thoughts on this step:

 



Copyright 2010, 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.