6. Turtle rectangle progressions
6.1. Sequencing: steps to draw a rectangle

t.seth(0)
t.goto(20, 30)
.t.pu()
and t.pd()
are used either side of it to avoid line drawing when repositioning the turtle.t.fd(120)
.t.lt(90)
.t.fd(50)
.t.lt(90)
.Code Completion: rectangle steps
Complete the code to draw a rectangle of side length 120 and width 50 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
XXX(20, 30)
t.pd()
t.seth(0)
XXX(120)
XXX(90)
XXX(50)
XXX(90)
XXX(120)
XXX(90)
XXX(50)
XXX(90)
s.exitonclick()
Completed code to draw a rectangle of side length 120 and width 50 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
s.exitonclick()
Tasks
From the code above, list the 8 lines that do the actual drawing.
From the 8 lines, list the simplest amount of code that is repeated to form a rectangle.
From the code above, list the lines that do the actual drawing.
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
From the 8 lines, list the simplest amount of code that is repeated to form a rectangle.
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
6.2. Iteration: using a for-loop to draw a rectangle
Code Completion: rectangle for loops
Complete the code to draw a rectangle of side length 120 and width 50 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
XXX(20, 30)
t.pd()
t.seth(0)
for _ in range(XXX):
XXX(120)
XXX(90)
XXX(50)
XXX(90)
s.exitonclick()
Completed code to draw a rectangle of side length 120 and width 50 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
for _ in range(2):
t.fd(120)
t.lt(90)
t.fd(50)
t.lt(90)
s.exitonclick()
Tasks
Modify the code above to draw a rectangle of 80 by 150.
Modify the code above to draw a rectangle of 80 by 150.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
t.pu()
t.goto(20, 30)
t.pd()
t.seth(0)
for _ in range(2):
t.fd(80)
t.lt(90)
t.fd(150)
t.lt(90)
s.exitonclick()
6.3. Definitions: using a def block to draw a rectangle
- rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0)
- t - the turtle object to draw the rectanglelength - side length; default 40width - side width; default 30start_pos - start position; default (0, 0)start_h - start heading; default 0 degrees
rectangle(t)
draws a default rectangle.rectangle(t, length=120, width=50, start_pos=(20, 30))
draws a rectangle of 120 by 50 at (20, 30).rectangle(t, length=400, width=300, start_pos=(-300, -100), start_h=10)
draws a rectangle of 400 by 300 at (-300, -100) with an angle of 10 degrees.Code Completion: rectangle definition
Complete the code to draw a rectangle of side length 120 and width 50 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
def rectangle(t, XXX=40, XXX=30, XXX=(0, 0), XXX=0):
t.pu()
t.goto(XXX)
t.pd()
t.seth(XXX)
for _ in range(2):
t.fd(XXX)
t.lt(90)
t.fd(XXX)
t.lt(90)
rectangle(t)
rectangle(t, length=120, width=50, start_pos=(20, 30))
rectangle(t, length=400, width=300, start_pos=(-300, -100), start_h=10)
s.exitonclick()
Completed code to draw a rectangle of side length 120 and width 50 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
def rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(2):
t.fd(length)
t.lt(90)
t.fd(width)
t.lt(90)
rectangle(t)
rectangle(t, length=120, width=50, start_pos=(20, 30))
rectangle(t, length=400, width=300, start_pos=(-300, -100), start_h=10)
s.exitonclick()
Tasks
Modify the code above to draw a rectangle of 80 by 150 at (-80, -150).
Modify the code above to draw a rectangle of 80 by 150 at (-80, -150).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(5)
def rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
for _ in range(2):
t.fd(length)
t.lt(90)
t.fd(width)
t.lt(90)
rectangle(t, length=80, width=150, start_pos=(-80, -150), start_h=0)
s.exitonclick()
6.4. Adding pen colour and fill colour parameters
- rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0, penw=1, penc='black', fillc=None)
- t - the turtle object to draw the rectanglelength - side length; default 40width - side width; default 30start_pos - start position; default (0, 0)start_h - start heading; default 0 degreespenw - pensize; default 1penc - pencolor; ; default “black”fillc - fillcolor; default None

rectangle(t, length=400, width=300, start_pos=(-100, -150), start_h=10, penw=5, penc="black", fillc="light green")
draws a rectangle of 400 by 300 at (x=-100, y=-150) with a black pencolor, a green fillcolor, using a pensize of 5.Code Completion: rectangle_steps_coloured definition
Complete the code to draw a rectangle of side length 120 and width 50 at (20, 30), by replacing the XXXs.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(0)
def rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0,
XXX=1, XXX="black", XXX=None):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(XXX)
t.pencolor(XXX)
if XXX is not None:
t.fillcolor(XXX)
t.begin_fill()
for _ in range(2):
t.fd(length)
t.lt(90)
t.fd(width)
t.lt(90)
if XXX is not None:
t.end_fill()
rectangle(t, length=400, width=300, start_pos=(-100, -150), start_h=10,
penw=5, penc="black", fillc="light green")
s.exitonclick()
Completed code to draw a rectangle of side length 120 and width 50 at (20, 30).
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(0)
def rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0,
penw=1, penc="black", fillc=None):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(penw)
t.pencolor(penc)
if fillc is not None:
t.fillcolor(fillc)
t.begin_fill()
for _ in range(2):
t.fd(length)
t.lt(90)
t.fd(width)
t.lt(90)
if fillc is not None:
t.end_fill()
rectangle(t, length=400, width=300, start_pos=(-100, -150), start_h=10,
penw=5, penc="black", fillc="light green")
s.exitonclick()
Tasks
Use the definition provided above to draw a rectangle of side lengths 150 and 250 at (x=-150, y=-250) with a purple pencolor, a bisque fillcolor, with a pensize of 10.
Use the definition provided above to draw a rectangle of side lengths 150 and 250 at (x=-150, y=-250) with a purple pencolor, a bisque fillcolor, with a pensize of 10.
import turtle
s = turtle.Screen()
s.bgcolor("white")
s.title("rectangle")
s.setup(width=800, height=600, startx=0, starty=0)
t = turtle.Turtle()
t.speed(0)
def rectangle(t, length=40, width=30, start_pos=(0, 0), start_h=0,
penw=1, penc="black", fillc=None):
t.pu()
t.goto(start_pos)
t.pd()
t.seth(start_h)
t.pensize(penw)
t.pencolor(penc)
if fillc is not None:
t.fillcolor(fillc)
t.begin_fill()
for _ in range(2):
t.fd(length)
t.lt(90)
t.fd(width)
t.lt(90)
if fillc is not None:
t.end_fill()
rectangle(t, length=150, width=250, start_pos=(-150, -250), start_h=0,
penw=10, penc="purple", fillc="bisque")
6.5. Practice Questions
Exercises
Using sequencing only, draw a rectangle of side lengths 500 and 400 at (-250, -250).
Using a repeat loop (without a function), draw a rectangle of side lengths 50 and 40 at (-25, -25).
Use the definition provided above to draw a rectangle of side lengths 400 and 300 at (x=-300, y=-200) with a black pencolor, a snow fillcolor, with a pensize of 6.