CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 9 - Repetition


As usual, create two directories for today's class. Create a directory called lecture9 under activities, and a directory called lab9 under labs.


Repetition

Up to now, in order to get repeated behavior we had to copy and paste code or call functions multiple times. That, however, is not the most efficient mechanism for getting repeated behavior. Today, we will learn about for loops.


In-class Activity 1

Polygons

In a file called polygon.py, create a function called draw_polygon that takes as parameters the x and y coordinates of the polygon, as well as the number of sides in the polygon. This function should use the turtle module to draw a polygon at the coordinates specified by the user with the specified number of sides.


The colors Duke, the colors!

Your turtle drawings have been limited to black lines on a white background. However, it is fairly easy to change the color that turtle draws in. Using the turtle.color function, we can start creating more appealing looking drawings.


Lab Assignment 9

Plotting

Create a function called draw_parabola in a file called parabola.py. The function should draw the function: y=x2/100 with the domain: −(w/2)<x<(w/2) and the range: −(h/2)<y<(h/2) where w and h are the width and height of the window. The functions window_height and window_width in the turtle module can be used to get the dimensions of the window.

You should draw the major axis in one color, and the plot of the function in another color. Play with the step to find a decent representation of the function.

If you structure your code well enough, you should be able to easily alter the function being plotted. Find a more interesting function, and attempt to plot it using your function. You will not get full points for the lab if your code plots the above function.


Submission

When you have finished, create a tar file of your lab9 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czf lab9.tgz lab9/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 9. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your file.

Do not forget to email your partner todays files!


Last modified: Mon Sep 16 09:42:14 EDT 2013