The purpose of this project is to give you chance to break down simple problems (such as drawing a logo) into precise steps that solve the problem (draw the logo). In computer science speak, you will be developing algorithms. In this case you will write your algorithms as sequences of Python turtle commands that make the turtle draw complicated shapes for you.


Setting up

If you haven't already set yourself up for working on the project, then do so now.

  1. Mount your directory on the Personal server.
  2. Open the Terminal and navigate to your project1 directory on the Personal server.
  3. Open TextWrangler. If you want to look at any of the files you have already created, then open those files.

Shape A


Invent a shape, such as a cross, or an L or a simple chair. The next instruction is to write Python code to make the turtle draw the shape, so keep the shape fairly simple.


Create a file called shapeA.py in your project1 directory. It should follow the same basic format as the python file we made in lab, so make sure the first and last lines in shapeA.py are the same (the import and input lines). Between those lines, put the turtle commands to draw the shape you just invented. Run the program (i.e. type python3 shapeA.py in the terminal) and take a picture of the shape (i.e. Shift-Cmd-4).


Include a picture of this shape in your write-up. This is required image 1.

You can take a screen shot by using Cmd-Shift-4 and then clicking and dragging the part of the image you want to capture. The file will be saved on your Desktop.

Shape B

Repeat your process for a second shape, putting its code in a file named shapeB.py.

Include a picture of the new shape in your write-up. This is required image 2.

Shape functions

Make a new Python file called shapes.py. As the name implies, this file will have code that draws more than one shape. We will begin by putting the code for shapes A and B into it as functions. We will then create a shape C function that is composed of shapes A and B.

shapeA( ) and shapeB( )

Create two functions shapeA() and shapeB(). A function is simply a collection of Python commands with a label, just like our lists above. For example, I could make a function that draws a triangle by use the following code.

def triangle( ):
   forward( 100 )
   left( 120 )
   forward( 100 )
   left( 120 )
   forward( 100 )
   left( 120 )

Note how the commands that make up the function are all tabbed in the same amount relative to the function definition. That is how you tell Python those commands are part of the function triangle(). In Python the visual indentation of the text has the semantic meaning that the indented text is part of the function labeled as triangle. Not all programming languages give semantic meaning to the visual organization of the code, but Python does. That means that the way Python code looks also has something to do with how it works.

Your shapeA() and shapeB() functions should draw the shapes A and B you defined in the prior step. You can copy and paste that code into the functions, tabbing in the code appropriately.

shapeC( )

Invent a shape C that is composed of shapes A and B. The code for shapeC() will call the functions for shapeA() and shapeB(). You can call a function in Python by using the function name followed by parentheses. For example, the following code creates a function called shapeC() that does three things. First it executes the code in the shapeA() function, then it moves the turtle forward by 100 pixels, then it executes the code in the shapeB() function.

def shapeC():
   forward( 100 )

input('Enter to continue')

Call your shapeC( )

At the end of your shapes.py file, put a call to the shapeC() function, as in the example above. That will tell Python to call the function when you run the shapes.py program. Note that the call to shapeC() is not tabbed in, and will therefore be executed by Python when you execute your file. Recall that the role of the final line of code is to keep the turtle window open until you are ready to let it go away. It does so by preventing the program from ending without you pressing Enter/Return.


Test your code. Run it by typing python3 shapes.py in the Terminal. If it doesn't work, then carefully read any error message. It should direct you to the line of the file that failed to work. Some common errors include the following:

Function parameters

The final task is to make your shape-drawing functions more flexible by enabling the caller to specify how many pixels forward to travel. For example, consider the triangle-drawing function above. Each triangle edge is always 100 pixels.

It would be better to have a triangle that could be drawn many different sizes. In other words, instead of using a raw, or hard-coded number to indicate the forward distance, it would be better to have the distance be adjustable. In programming terminology, this means we would like to use a parameter for the distance instead of a hard-coded number. You can create parameters for functions that allow you to pass information into a function. In the function definition, you put a list of parameter names inside the parentheses. You can then use the parameter names within the function.

When you call a function with parameters, you put the value of the parameters in the parentheses. For example, the following code defines a triangle and then calls it with two different values.

def triangle( distance ):
   forward( distance )
   left( 120 )
   forward( distance )
   left( 120 )
   forward( distance )
   left( 120 )

triangle( 50 )
triangle( 100 )

Shapes D and E

In the file shapes.py, create a shapeD() function that draws a shape (like shapeA() or shapeB()) and uses one of more parameters in the forward commands, just like the triangle function above.

Also in the file shapes.py, write a shapeE() function that calls the shapeD() function multiple times with different parameters.

Test your code. This means run it and make sure it works. If it doesn't work, then carefully read any error message. It should direct you to the line of the file that failed to work. In addition to the errors listed above, you may see the following error.

Argument/Parameter Mismatch error: Having a mismatch between the number of values passed in to a function when it is called and the number of parameters that function has defined (e.g. the triangle function needs 1 parameter, so both lines below will cause errors because the actual function wants one parameter.

triangle( 4, 3 )

Include a picture of the new shape in your write-up. This is required image 3.


Each assignment will have a set of suggested extensions. The required tasks and writeup constitute about 83% of the assignment. If you do only the required tasks and writeup -- and to them well -- you will earn a B. To earn a higher grade, you need to undertake one or more extensions. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. One complex extension, done well, or 2-3 simple extensions are typical.

These are only examples to help you start thinking of the unlimited possible ways you could extend the project. You are strongly encouraged to design your own extensions to suit your interests and show off your computational thinking skills.

Whichever extensions you choose, be sure to discuss your motivation, design process, implementation, and results in the writeup. A screenshot of your results is usually a great idea.

Writeup and Hand-in

There are two parts to handing in your project: handing in your code, and writing a brief report on the wiki.

Turn in your code

You will turn in your code by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151. Within the CS151 directory is a directory with your user name. Within this directory is a directory named Private. Files that you put into that Private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, you will create a new directory inside the Private directory, such as project1, and then copy your code into the project directory for that week. Note: This directory may not be available during lab, but will become available during the week, before the projects are due.

There are two ways to mount the appropriate directory.

Turn in your code by putting all of the necesary files into a project1 directory inside of your Private directory on the Courses server. To hand in your project code, copy shapeA.py, shapeB.py, and shape.py to your project1 directory on the Courses server. If you wrote additional Python files, you should hand them in as well. We will grade all files turned in, so please do not turn in old, non-working, versions of files.

Write Your Report

In lab, you made a new wiki page for your assignment. Put the label cs151s18project1 in the label field on the bottom of the page. But give the page a meaningful title (e.g. Caitrin's Project 1).

In general, your intended audience for your write-up is your peers not in the class--for instance, a student who will take the course next semester. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use the wiki report to explain to friends what you accomplished in this project and to give them a sense of how you did it. Follow the outline below.