CS 151: Lab #3

Lab Exercise 3: Conditionals and Iteration

The purpose of this lab time is to give you more practice with python and writing programs, and practice with designing and writing loops.


  1. Connect to your network directory. When connecting, always type fileserver1 into the connect box. Don't use the smb://... option in the big window.
  2. Create a new file in BBEdit, or whatever editor you wish. Save the file as <username>_shapes.py, where <username> is your Colby username. My file is called bmaxwell_shapes.py
  3. Write a function that draws a very simple shape (like a line) that fits within a 20x20 pixel area. Make sure the function puts the turtle back in its original location and orientation after making the drawing. Use local variables to remember the old location and orientation.
  4. Create a new file called lab3.py. Import turtle, turtleUtils, random, and your shapes file into the new file.
  5. Create a main() function in lab3.py that takes no arguments. Have the function just print out something right now. Then call the function on the top level of lab3.py (no tabs). Run the file.
  6. Put a for loop in the main function that loops 10 times. Inside the for loop move the turtle to a random location (without drawing) and have it draw a shape. The random() function returns a random value between 0.0 and 1.0.
  7. Create a second very simple shape in your shapes file.
  8. Within the for loop in the main() function, use the random() function to generate a value. If the value is less than 0.5 draw the first shape, otherwise draw the second. Use an if statement to take care of the control flow. Run the program.

Once you've finished the above exercise, get started on the next project.