Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Functions: There’s a Name for That 169

draw_spiral(-x,y, size)
draw_spiral(-x,-y, size)
draw_spiral(x,-y, size)


At u, we name our function draw_kaleido, and we allow it to
take the two parameters, x and y, from the turtle.onscreenclick()
event handler so that our four reflected spirals will start at the
(x, y) location where the user clicked the mouse. Then, at v, we
randomly choose a pen color for all four reflected spirals in a set
from our usual list of colors, colors.
At w, we pick a random size for all four reflected spirals and
store it in size. Finally, we draw all of the four spirals at their
(x, y), (–x, y), (–x, –y), and (x, –y) locations with a new function
we’ve yet to actually write, called draw_spiral().


The draw_spiral() Function


Our draw_spiral() function will need to draw a spiral starting at
a custom (x, y) location on the screen. Python’s turtle pen will
remember the color once it’s set, so we don’t have to pass that
information as a parameter to our draw_spiral() function, but we
do need the (x, y) location and the size of the spiral that we want
to draw. So we’ll define our draw_spiral() function to take three
parameters:


def draw_spiral(x,y, size):
t.penup()
t.setpos(x,y)
t.pendown()
for m in range(size):
t.forward(m*2)
t.left(92)

Free download pdf