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.
- Connect to your network directory. When connecting, always type fileserver1 into the connect box. Don't use the smb://... option in the big window.
- 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
- 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.
- Create a new file called lab3.py. Import turtle, turtleUtils, random, and your shapes file into the new file.
- 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.
- 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.
- Create a second very simple shape in your shapes file.
- 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.