
The goals this week are to:
Create a Lab03 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)
Open VS Code. Create a new Python source code file calledlab3.py. Import the random and turtle modules.
Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab03 folder.
Let's use conditionals to control whether a rectangle is filled with color.
goto and rectangle from last week's projectIf you have not already created a function that draws a rectangle with position parameters, no problem, take a moment and write one now.
The function signature should be:
def rectangle(x, y, width, height): '''Draws a `width` x `height` rectangle with bottom-left corner positioned at (`x`, `y`). ''' # your code here
Important: For full credit, your implementation MUST use a for loop. The reason is that this makes your code more readable, concise, maintainable, and efficient.
rectangleTest out your rectangle function out in main code by creating 3 rectangles:
Here is an example of what you should get:

rectangleName the parameter fill and give it a default value of False. Have the function fill the shape if the fill parameter is True.
Remember to stop filling by the end of the function, but only if you started filling in the first place. Hint: You can check the fill argument or this turtle function.
Name the parameters edgeColor and fillColor. Give them default values (edgeColor should be black by default). For the moment, assume that colors coming in will be strings (e.g. 'red').
Change the turtle's pen color using the two parameter version of the color function to specify the edge and interior colors, respectively. For example, if I wanted to change the pen edge color to red and the interior color to blue, I would write turtle.color('red', 'blue'). Remember to always change the pen color back to black by the end of the function.
rectangleThis will control the thickness of the lines that the turtle draws. Call the parameter penWidth. Give it a default value (Recall: The width value you are used to seeing is 1).
Set the turtle's pen width according to the argument value and reset it to 1 at the end of the function.
main functionLet's write a function that uses your customized rectangles to make a Mondrian pattern.
Create a function called main in lab3.py. In it, create a for loop that randomly places 200 rectangles of random sizes within your canvas. Use an if statement to fill 40% of the rectangles. Don't change the default colors for now. Generate random values for the other rectangle function parameters. Here are some suggestions:
At the very bottom of your program, use the following conditional statement to make sure that main is only called when lab3.py is run directly from Terminal:
if __name__== '__main__': main()
This if statement should be the only main code that you have from now on. In other words, the "if" should be on the only thing (other than function definitions and your imports at the top) that should have no indentation. Anything that you are tempted to put in main code should now be indented inside if __name__== '__main__':.
main functionAdd a call to exitonclick after calling main in the new conditional statement (indented) to make sure the canvas window stays up after the turtle is done drawing.
Tips:
turtle.speed(0) before the call to main to speed up the drawing.turtle.tracer(False), again right before the call to main.
turtle.update() right before exitonclick.Running lab3.py should give you something that looks like (my default fill color is red):

Using English words to name colors (red, blue, pink, etc.) is convenient, but it does not make it easy to randomly change the fill color in our rectangles! Any color of light can be produced by mixing red, green, and blue together in different proportions. For example, (0.3, 0, 0.9) would mean: make a color with 30% red, 0% green, and 90% blue. At the extremes, (0, 0, 0) would produce black and (1, 1, 1) would produce white.
You can specify colors to turtle as these RGB tuples (Red, Green, Blue). For example, instead of turtle.color('blue') you could write turtle.color((0, 0, 1)). Note the double parentheses: one set for the function call, the other to surround the RGB tuple.
In your loop within main, generate 3 random floats between 0 and 1 and specify the fill of your rectangle using a tuple containing these values.
Note: Don't forget the parentheses () when creating a tuple. This would work: fillColor=(0.1, 0.2, 0.3), but this would not work: fillColor=0.1, 0.2, 0.3
You should get something that looks like:

Connect to Filer and navigate to /Courses/CS151_A_FA/<your username>/Private/Projects/<your username>_P03.
Copy the lab3.py file into the <your username>_P03 folder on Filer.
Once you are done with each of the lab tasks, you may start on the project.
© 2021 Oliver Layton