Project 1: What is a Shape?
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.
Project 1 Concept Outline
- [Go] Write a program to draw a shape in a file called shapeOne.py, run the program and take a screenshot of the shape.
- [Go] Write a program to draw a different shape in a file called shapeTwo.py, run the program and take a screenshot of the shape.
- [Go] Write a program in a file called shapes.py, that draws a shape which is a combination of two other shapes. Run the program and take a screenshot of the image.
This program will have a function for each shape; shapeOne() and shapeTwo(). It will also have a function called shapeThree() that creates a composite shape that uses shapeOne and shapeTwo, calling their respective functions. The final program should call the shapeThree() function.
- [Go] Modify shapes.py to draw shapes at different scales and take a screenshot of the image. Then create a function shapeFour() that takes in a distance parameter to scale the shapes. Write a function shapeFive() which calls shapeFour() with different parameters. The final program should call the shapeFive() function.
- [Go] Write a report of the project on your wiki. Include the images.
- [Go] Copy shapeOne.py, shapeTwo.py, shapes.py to your handin directory.
- Set up your workspace
If you haven't already set yourself up for working on the project, then do so now.
- Mount your directory on the Personal server.
- Open the Terminal and navigate to your project1 directory on the Personal server.
- Open TextWrangler. If you want to look at any of the files you have already created, then open those files.
- Design your first shape [A]
Invent a shape, such as a cross, or an L or a simple chair. Do this on a piece of paper or white board. You mmay even want to use graph paper so you can count the length of the sides of the shape.
The next instruction is to write Python code to make the turtle draw the shape, so keep the shape fairly simple.
- Write the code for your first shape
Create a file called shapeOne.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 shapeOne.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 shapeOne.pyin the terminal) and take a picture of the shape (i.e. Shift-Cmd-4).
Include a picture of this shape in your report. 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.
- Design and write the code for your second shape [B]
Repeat your process for a second shape, putting its code in a file named shapeTwo.py.
Include a picture of the new shape in your report. This is required image 2.
- Convert your shapes to functions and make a more complex shape [C]
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 shapeOne and shapeTwo code into it as functions. We will then create a shapeThree function that is composed of shapes One and Two.
Create two functions shapeOne and shapeTwo. 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 shapeOne and shapeTwo functions should draw the shapeOne and shapeTwo shapes you defined in the prior step. You can copy and paste that code into the functions, tabbing in the code appropriately.
Invent a shapeThree that is composed of shapeOne and shapeTwo. The code for shapeThree will call the functions for shapeOne and shapeTwo. You can call a function in Python by using the function name followed by parentheses. For example, the following code creates a function called shapeThree that does three things. First it executes the code in the shapeOne function, then it moves the turtle forward by 100 pixels, then it executes the code in the shapeTwo function.
def shapeThree(): shapeOne() forward( 100 ) shapeTwo() shapeThree() input('Enter to continue')
- At the end of your shapes.py file, put a call to the shapeThree 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 shapeThree 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 (i.e. run it by typing
python3 shapes.pyin 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.
- Syntax error: Forgetting the colon at the end of a function definition line
- Syntax error: Forgetting to pair a left parenthesis with a right parenthesis. Without a closing parenthesis, Python doesn't know where to stop reading.
- Typo: Misspelling a turtle command or function name. These are some of the most difficult bugs to spot. Fortunately, Python will tell you the line number where the error is located. The error will often be that the misspelled symbol (word or name) has not been defined.
- Tabbing/White Space error: 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" a function definition should be tabbed in once, with all lines tabbed in the same amount.
Note that Python considers tabs and white space to be different things, which is sometimes hard to debug because all of your code looks correct. If you think your code is correct, then select all of your code (cmd-A) and then choose Text::Entab in TextWrangler to convert all of your white space to tabs or Text::Detab to convert all of your white space to spaces. That will generally correct the problem if the error is mis-matched white space.
- Create two functions shapeOne and shapeTwo. 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.
- Make your functions more powerful by adding parameters [D]
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 )
- In the file shapes.py, create a shapeFour function that draws a shape (like shapeOne or shapeTwo) and uses one of more parameters in the forward commands, just like the triangle function above.
- Also in the file shapes.py, write a shapeFive function that calls the shapeFour 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() triangle( 4, 3 )
Include a picture of the new shape in your report. This is required image 3.
- What is a variable?
- What is a function and how are they useful?
- What is a parameter and how do they affect the generality of a function?
- How can you use functions to create complexity with less code?
- Pick a wall drawing by Sol LeWitt that you think is interesting. Why? Do you think you could write code for it in python?
Projects are your opportunity to learn by doing. They are also an opportunity to explore and try out new things. Rather than list out every activity for a project, the defined part of each project will be about 85% of the assignment. You are free to stop there and hand in your work. If you do all of the required tasks and do them well, you will earn a B/B+.
To earn a higher grade, you can undertake one or more extensions. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. Extensions are your opportunity to customize the assignment by doing something of interest to you. Each week we will suggest some things to try, but you are free to choose your own.
A common question is "how much is an extension worth?" The answer to that question depends on the extension, how well it is done, and how far you pushed it. As teachers, we prefer to see one significant extension, done well, where you explore a topic in some depth. But doing 2-3 simpler extensions is also fine, if that's what you want to do. Choose extensions that you find interesting and challenging.
The following are a few suggestions on things you can do as extensions to this assignment. You are free to choose other extensions, and we are happy to discuss ideas with you.
- 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.
- Creative or complex scenes above and beyond the basic expectations count as extensions.
Submit your code
Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, 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, copy your code into the pre-existing project directory for that week. Please submit only code that you want to be graded.
When submitting your code, double check the following.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
- Is your handin project directory inside your Private folder on Courses?
Write Your Project Report
If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Please use the CS 151 Template. Give the page a meaningful title (e.g. Milo's CS 151 Project 1). Enter the label
cs151s20project1 in the label field on the bottom of the page.
Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports, be please minimize references to them. Your goal should be to be able to use your report to explain to friends what you accomplished in this project and to give them a sense of how you did it.
Your project report should contain the following elements. Please include a header for each section.
A brief summary of the project, 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. Each assignment will have both a core CS purpose--such as using variables or functions--and an application such as making images in the style of an artist.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application (e.g. making images)?
- Does it describe your the solution or how it was developed (e.g. what code did you write)?
- Does it describe the results or outputs (e.g. did your code work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
For each task, If you completed the task, include an image of the completed task and write a few sentences describing the image/what you did. This should be a description of the form and functionality of your final code. Note any unique computational solutions you developed or any insights you gained from your code's output.
If you didn't complete the task insert an image showing how far you got. Write a few sentences describing how much you completed, where you got stuck and what what solutions you tried.
You may want to incorporate code snippets in your description to point out relevant features. 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 text output or images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
- Follow-up Questions
The answers to any follow-up questions (there will be 3-4 for each project).
A brief description (1-3 sentences) of what you learned. Think about the answer to this question in terms of the stated purpose of the project. What are some specific things you had to learn or discover in order to complete the project?
A list of people you worked with, including TAs and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
A list of any resources that you used. This includes textbooks, lecture notes, python documentation, library documentation or forums. If you used a specific website, please link to it.
- Put the label cs151s20project1 on your wiki page.