It’s Hip to be Square!

The Challenge

A few lessons ago, we worked with function drawSquare(), which draws a square of the specified size. Today your challenge is to use that function, along with another function you will create called drawPattern(), to create the following cool pattern:


Details for drawPattern()

In addition to stealing the drawSquare() function (here), we also need to create a function that will create not only this pattern, but all similar patterns formed by any number of squares.  (Have you counted the number of squares in the pattern above yet?)

So, here are the details of drawPattern():

  • drawPattern() receives three parameters, t, num, and sz.
  • t is our turtle, num is the number of squares in the pattern, sz is the side length of the squares.
  • drawPattern() should include a loop that repeats calling drawSquare() “num” times, then turns left a calculated number of degrees.

You’ll have to do some math to decide how far to turn left.  By that I mean, a formula.  First recall the number of degrees in a full rotation (circle).  Now, answer this question: If I want 20 squares in a circle, how many degree are between each square?  Extend that math to the variable num.  If we want num squares around a circle, what math do we do to figure out how many degrees the squares a separated by. Hint: You’ll need division.

The Assignment

Write a complete program to draw the awesome pattern in the image above. Your program must include and use two functions, drawSqaure() and drawPattern().

Here’s the plan:

  • First, import turtle.
  • Next, add the drawSquare() function to your program.
  • Now add your drawPattern() function.
  • Finally, in your main program, create a screen and a turtle, set your colors, and call you drawPattern() function with the appropriate parameters (your turtle, the number of squares, and the length of the side of the square).

Make sure your program has appropriate comments, including a block for function definitions and a block for the main program code (see below):

# ***************************************************************
# Functions
# ***************************************************************

def drawSquare(t, sz):
    """Make turtle t draw a square of with side sz."""

    # Put code for drawSquare() here
    # More code

def drawPattern(t, num, sz):
    """Draw a pattern of num squares of size sz."""

    # Put code for drawSquare() here
    # More code

# ***************************************************************
# Main Program Code
# ***************************************************************

# Put main program code here and below

Save this file with the name  When the program is running correctly, submit it using the online submission here.

Bonus Time

If time permits, extend the program to accept input from the user, specifically, ask the user to enter the number of squares and the side length suing raw_input statements, then create the screen, turtle, etc. and call drawPattern().

Save this file with the name  When the program is running correctly, submit it using the online submission here.