A Mondrian Collage
CS151 – Lab 03
loops, conditionals, optional parameters, random
Due: 02/26/2019
The goals of this lab are to practice controlling the algorithmic flow of our programs with loops and conditionals. We’ll do this by improving the efficiency of your block() function, drawing multiple blocks in a turtle image, and using loops to save ourselves a lot of typing, and using function optional parameters to easily customize the number and appearance of your shapes.
Now we're going to explore the range() function, which is helpful for controlling for loops in Python.
for i in range(5): print(i)
What does it print? What is the value of the loop variable at each iteration of the loop?
10. Copy and paste your block() and goto() functions from last week’s Project into lab3.py.
11. If your block does not have a scale parameter, add it so that you can make your block any size. Your block function should have at least the following parameters: block(x, y, s, width, height)
12. Use a loop to reduce the total amount of code that the block() function requires to draw a rectangle.
As an example, here are two ways to write the star() function from Lab 03:
def star_inefficient( x, y, size ):
‘’’This function draws a star using 13 lines of
code. Not terrible, but we can do better using
loops and functions. ’’’
turtle.up()
turtle.goto( x, y )
turtle.down
turtle.forward( size )
turtle.left( 144 )
turtle.forward( size )
turtle.left( 144 )
turtle.forward( size )
turtle.left( 144 )
turtle.forward( size )
turtle.left( 144 )
turtle.forward( size )
turtle.left( 144 )
def star( x, y, size ):
‘’’The same star with only 4 lines of code!’’’
goto( x, y ) # you’ve written a function for this!
for i in range(5):
turtle.forward( size )
turtle.left(144)
If you call your goto() function, then the star() function you wrote in lab requires only 4 lines of code to draw the same shape as star_inefficient(). That’s 70% less code than it took to draw a star without taking full advantage of loops and function calls. One of the main reasons why we’re studying loops is to save you all that typing!
13. Add an optional parameter called fill to your block() function. You can make a parameter optional by giving it a default value. If the user doesn’t pass in a value, the function will use this default. In the body of the block function, if fill is True, then fill the shape with color. Otherwise, don’t fill the shape.
Hint: We recommend using two if statements: one before drawing your shape to check whether the turtle should begin filling, and one after drawing your shape to check whether the turtle should stop filling. That way you don’t need two complete copies of the code that draws your shape (one filled and one empty), which would be inefficient (more typing).
14. Add optional parameters for fillColor and borderColor to your block function. Then, in the beginning of your function, use the two parameter version of the turtle.color(borderColor, fillColor) function to independently control the interior fill and border color of the block. Set the default color for your border to be black.
15. Add an optional parameter to control the turtle pen size in your block function. At the beginning of your function change the turtle pen size to the parameter value. Set the pen width back to its default value (1) at the end of your function.
Color hints and helpful links:
16. 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 the Terminal:
if __name__== ”__main__”:
main( )
This if statement will only evaluate to True if lab3.py is executed from the Terminal, not if it is imported in another program as a library. That means we’ll only call main() if the user runs lab3.py directly.
17. Just above this if statement, define your main() function:
def main( ):
In your main() function, create a for loop that randomly places 200 blocks of random sizes within your window Use an if statement to fill 40% of the blocks with random fill colors. Generate random values for your block function parameters. Here are some suggestions:
Running lab3.py that executes your main() function should generate a Mondrian collage pattern.
Your Mondrian collage is Required Image 0 in your report.