Functions Lab | Part II
In this lab, we will continue to practice creating and calling functions.
 Set up
This lab will use the previous lab directory.💻 In the Terminal, type the following command to open the lab folder.
💻 Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.
👾 💬 Exiting the poetry shell
When you want to exit the shell, you can type
💻 We will only be working with:
 Draw Grid
Let’s practice writing some more functions. Our aim is to create a grid of squares.
This is a complex problem, and thus we should use the practice of decompsition to break it down into smaller steps.
- Step 1: Drawing one square
- Step 2: Drawing one line of squares
- Step 3: Drawing a grid of squares
👾 💬 Many Solutions...
There are many solutions to this problem. If you feel confident to solve this problem in different way and without hints, please feel free to do so.
We will accept any solution that draws a grid of squares of any square size and any gap size when you run
[Helpful Turtle Functions]
Here are some helpful Turtle functions that may make this problem easier. You are not required to use all of these functions.
|setheading()||setheading(0)||Sets the orientation of the turtle to an angle. 0 is east; 90 is north; 180 is west; 270 is south|
|goto()||goto(0,0)||Moves the turtle to an absolute x,y position|
|home()||home()||Moves the turtle to the start position (0,0)|
|penup()||penup()||Lifts the turtle pen up so it doesn’t draw|
|pendown()||pendown()||Lifts the turtle pen down so it doesn’t draw|
|speed()||speed(1)||Changes the speed of the turtle. 0 is the fastest. 1 is the slowest.|
[Step 1]💻 Open the file:
💻 Fill in the
square(square_size) function. It should draw one square of any size.
square() function. Make sure it can draw a square of any size.
💻 Implement the STEP 2 functionality in the
line_of_squares(square_size, gap_size) function. Draw one line of squares the length of
square_size with gap the size of
You should use your
square()function to create a line of squares.
line_of_squares() function. Make sure it can draw a line of squares of any
💻 Implement the STEP 3 functionality in the
grid(square_size, gap_size) function.
You should use your
line_of_squaresto create a grid of squares.
grid() function. Make it sure it can draw a 4x4 grid of squares.
If you did not fill out the Google form in “Function I”, be sure to fill out this Google form.
[Custom Grid]💻 Expand the functionality of the program so the user can customize the following:
- number of rows
- number of columns
- color palette
Here is an example interaction:
python grid.py --- PATERN GENERATOR --- How many rows? > Enter a number: 5 How many columns? > Enter a number: 3 Pick a primary color. > Choose a color (darkseagreen, coral, deeppink): darkseagreen Pick a secondary color. > Choose a color (darkred, coral, cyan, cadetblue): coral [Press any key to exit]