A Mondrian Collage

CS151 – Lab 03

loops, conditionals, optional parameters, random

Due: 02/26/2019

Overview & Purpose

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.

Functions & Keywords to Review

Python

0. Setup

  1. In your Personal directory, make a folder called project3.
  2. Start a new file called lab3.py, and save it in your project3 folder.
  3. Make sure the top of your file has a descriptive file header: a comment with your name, the date, the file name, and a brief description of the file’s purpose (e.g. CS151 Spring 2019 Lab 03: Practicing loops, conditionals, and optional parameters).
  4. Import the turtle and random modules near the top of your program, just below your file header. 

1. Exploring range( )                                

Now we're going to explore the range() function, which is helpful for controlling for loops in Python.

  1. Open up the Python interpreter in a terminal by typing python3 <Enter> (no python filename after the command like usual).
  2. Type the following code into the interpreter and run it:

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?

  1. Try giving the range function two arguments (e.g. range(5, 10)). What does it print now? Try several different pairs of arguments, just to get a feel for it.
  2. Try giving the range function three arguments (e.g. range(5, 10, 2)). What does it print now? Try several different sets of arguments.
  3. Search for “Python 3.7 range” in your favorite Internet search engine, and check out the incredibly helpful range entry in the official Python 3 documentation. (Make sure you select the search result for the Python 3 docs, and not Python 2!) How does Python’s tutorial mesh with what you’ve discovered about range()?

2. Better blocks

2.a. Efficient blocks with loops

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!

2.b. Filled blocks with conditional statements

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).

2.c. Colorful blocks

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:

  1. You can read all about using fill and pen colors (including examples!) in the official Python 3 Turtle docs.
  2. In the Turtle docs you’ll learn that each color can be represented by a string containing its English name or by an RGB tuple -- a group of three numbers in parentheses that specify the ratio of Red to Green to Blue that define a color. For instance, the string “Lavender” and the RGB tuple (0.90, 0.90, 0.98) are interchangeable to the Python turtle. The strings are more human-friendly, but the RGB tuples give you an opportunity to use math or the random package to give your program the power to generate colors automatically!
  3. Here is a list of the color names that the Python Turtle can understand, along with the RGB ratios that define these colors. (You’ll have to convert percentages like 98% to floats like 0.98 for the turtle.)

3. A Mondrian Collage

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.

You’re ready to start the project!