Lesson 4 Slides
Lesson 4 Slides
Lesson 4
Topics
Functions:
# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
# move pen
my_ttl.penup()
my_ttl.goto(-100, 0)
my_ttl.pendown()
# draw square
for index in range(4):
my_ttl.forward(200)
my_ttl.right(90)
# draw triangle
for index in range(3):
my_ttl.forward(200)
my_ttl.left(120)
# move pen
my_ttl.penup()
my_ttl.goto(-25, -200)
my_ttl.pendown()
# draw rectangle
for index in range(2):
my_ttl.forward(50)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)
# move pen
my_ttl.penup()
my_ttl.goto(-80, -100)
my_ttl.pendown()
# draw square
for index in range(4):
my_ttl.forward(35)
my_ttl.right(90)
# move pen
my_ttl.penup()
my_ttl.goto(45, -100)
my_ttl.pendown()
# draw square
for index in range(4):
my_ttl.forward(35)
my_ttl.right(90)
# move pen
my_ttl.penup()
my_ttl.goto(15, -150)
my_ttl.pendown()
# draw circle
my_ttl.circle(5)
my_ttl.hideturtle()
Look at comments
def move_pen():
my_ttl.penup()
my_ttl.goto(-100, 0)
my_ttl.pendown()
...
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
move_pen()
def move_pen():
my_ttl.penup()
my_ttl.goto(-100, 0)
my_ttl.pendown()
Magic numbers → x and y the coordinates
def move_pen():
my_ttl.penup()
my_ttl.goto(x, y)
my_ttl.pendown()
import turtle
...
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
move_pen(-100, 0)
Investigate the code
first value → x
second value → y
move_pen(-100,0) :
0 → second value ( y )
import turtle
# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
move_pen(-100, 0)
# draw square
for index in range(4):
my_ttl.forward(200)
my_ttl.right(90)
# draw triangle
for index in range(3):
my_ttl.forward(200)
my_ttl.left(120)
move_pen(-25, -200)
# draw rectangle
for index in range(2):
my_ttl.forward(50)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)
move_pen(-80, -100)
# draw square
for index in range(4):
my_ttl.forward(35)
my_ttl.right(90)
move_pen(45, -100)
# draw square
for index in range(4):
my_ttl.forward(35)
my_ttl.right(90)
move_pen(15, -150)
# draw circle
my_ttl.circle(5)
my_ttl.hideturtle()
Line count: 71 → 63
Testing tips
Good to test frequently
Change code → test it
Too many changes between testing → harder to debug
Function passes test
don't need have to testing again (unless changed)
error elsewhere in code
Functions in Flowcharts
Flowcharts don't represent whole programs → represent algorithms
Reasons:
def draw_square(length):
for index in range(4):
my_ttl.forward(length)
my_ttl.right(90)
# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
move_pen(-100, 0)
draw_square(200)
# draw triangle
for index in range(3):
my_ttl.forward(200)
my_ttl.left(120)
move_pen(-25, -200)
# draw rectangle
for index in range(2):
my_ttl.forward(50)
my_ttl.left(90)
my_ttl.forward(100)
my_ttl.left(90)
move_pen(-80, -100)
draw_square(35)
move_pen(45, -100)
draw_square(35)
move_pen(15, -150)
# draw circle
my_ttl.circle(5)
my_ttl.hideturtle()
Line count: 63 → 55
There is no more repetition in the main code, but there is still three code blocks
remaining. Notice how the rest of the code is easier to read? Therefore, we will
transform the # draw triangle , # draw rectangle and # draw circle code blocks
into functions.
See if you can change all three blocks into functions. Remember to test each function
when you create it.
import turtle
def draw_square(length):
for index in range(4):
my_ttl.forward(length)
my_ttl.right(90)
def draw_triangle(length):
for index in range(3):
my_ttl.forward(length)
my_ttl.left(120)
def draw_circle(rad):
my_ttl.circle(rad)
# set up screen
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
##################################
## Using the tutrle command you ##
## have learnt, draw a house. ##
##################################
move_pen(-100, 0)
draw_square(200)
draw_triangle(200)
move_pen(-25, -200)
draw_rectangle(100, 50)
move_pen(-80, -100)
draw_square(35)
move_pen(45, -100)
draw_square(35)
move_pen(15, -150)
draw_circle(5)
my_ttl.hideturtle()
Line count: 71 → 59
Easier to read, test and debug
Exercises
Exercises are the make component of the PRIMM model
import turtle
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
sides = 9
length = 100
draw_poly(length, sides)
PRIMM
To do this change:
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
draw_poly(length, sides)
("How many sides?> ") → what to write before waiting for input
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
draw_poly(length, sides)
PRIMM
Complete exercise 3