CS 151 Project 1

### Project 1: Hello, Turtle

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

1. 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 Proj1 directory on the Personal server.
3. Open TextWrangler. If you want to look at any of the files you have already created, then open them.
2. 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 you will want to keep the shape fairly simple.
3. Create a file shapeA.py in your Proj1 directory. It should follow the same basic format as simple_turtle.py, so make sure the first and last lines in shapeA.py are the same as the first and last lines in simple_turtle.py. Between those lines, put the turtle commands to draw the shape you just invented. Run the program (i.e. type python 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 2 (the first is the one we took in lab).

4. 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 3.

5. 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 shapeA and shapeB code into it. We will then create a shapeC that is composed of shapes A and B.
1. 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.

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

2. Invent a shape C that is composed of shapeA and shapeB. 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:

```def shapeC():
shapeA()
forward( 100 )
shapeB()

shapeC()
raw_input('Enter to continue')
```
3. At the end of your shapes.py file, put a call to the shapeC function, as in the example above, so that it will 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 run your file as below. 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.

4. Test your code (i.e. run it by typing python shapes.py). 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
• Forgetting the colon at the end of a function definition line
• Forgetting to pair all parentheses (Python really hates it when you have more left/open parentheses than right/close parentheses)
• Misspelling a turtle commmand or function name
• Having inconsistent tabbing. All code should be lined up carefully. The main code should have no spaces or tabs at the beginning of the line. The code "inside" the function definition should be tabbed in once, with all lines tabbed in the same amount.
6. 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.

But it would be better to have a triangle that could be drawn many different sizes. In other words, instead of using a raw 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 variable 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 variable names inside the parentheses. You can then use the parameter variables 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 )
```
1. Create a shapeD function that draws a shape (like shapeA or shapeB) and uses one of more variables in the forward commands, just like the triangle function above.
2. Then write a shapeE function that calls the shapeD function multiple times with different parameters. At the end of your file, make sure to put a call to shapeE.
3. 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. Some common errors include
• Forgetting the colon at the end of a function definition line
• Forgetting to pair all parentheses (Python really hates it when you have more left/open parentheses than right/close parentheses)
• Misspelling a turtle commmand or function name
• Having inconsistent tabbing. All code should be lined up carefully. The main code should have no spaces or tabs at the beginning of the line. The code "inside" the function definition should be tabbed in once, with all lines tabbed in the same amount.
• 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 (e.g. the triangle function needs 1 parameter, so both lines below will cause errors:
```triangle()
triangle( 4, 3 )
```

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

### Extensions

Each assignment will have a set of suggested extensions. The required tasks constitute about 85% of the assignment, and if you do only the required tasks and do 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.

The following are a few suggestions on things you can do as extensions to this assignment. You are free to choose other extensions.

• Create some additional functions that draw different shapes. Create a function that calls all of them to make an interesting graphic.
• Write a function that will draw an N-gon. It should take in the distance of a side and the number of sides.
• Figure out how to have a set of instructions repeat a certain number of times.
• Build a hierarchy of functions. For example, suppose you already have a function shapeG that draws one shape. Make a new function shapeH that calls function shapeG multiple times, but in different places, so that you get multiple shapes. Then make another function shapeJ that calls shapeH multiple times. Go up one more level of complexity.

### Writeup and Hand-in

Turn in your code by putting it into your private directory on the Courses server. During lab you simulated turning in simple_turtle.py. Now, copy shapeA.py, shapeB.py, and shape.py to your Proj1 directory on the Courses server. If you wrote additional Python files, you may turn them in as well. We will grade all files turned in, so please do not turn in old, non-working, versions of files.

Next, expand on the wiki page your began in lab. In general, your intended audience for your write-up is your peers not in the class. Your goal should be to be able to use it 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.

• A brief summary of the task, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment.
• A description of your solution to the tasks, including any images you created. This should be a description of the form and functionality of your final code. For this project, you should also include a description of the programming process, and refer to your first image. You may want to incorporate code snippets in your description to point out relevant features. Note any unique computational solutions you developed. Code snippets should be small segments of code--usually less than a whole function--that demonstrate a particular concept. If you find yourself including more than 5-10 lines of code, it's probably not a snippet.
• A description of any extensions you undertook, including images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
• A brief description (1-3 sentences) of what you learned.
• Double-check the label. In lab, you should have added a the label cs151s13project1 to your wiki page. Make sure it is there.