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

(vip2019) #1

54 Chapter 4


Figure 4-1: A four-circle rosette pattern

Let’s think about how we might write a program to draw four
circles overlapping as shown. As you saw in Chapter 2, Turtle’s
circle() command draws a circle with the radius we specify inside
its parentheses. Those circles look like they’re at the north, south,
east, and west of the screen, 90 degrees apart, and we know how
to turn left or right 90 degrees. So we could write four pairs of
statements to draw a circle, then turn 90 degrees, and then draw
another circle, as in the following code. Type this into a new win-
dow and save it as Rosette.py.

Rosette.py

import turtle
t = turtle.Pen()
t.circle(100) # This makes our first circle (pointing north)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our second circle (pointing west)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our third circle (pointing south)
t.left(90) # Then the turtle turns left 90 degrees
t.circle(100) # This makes our fourth circle (pointing east)
Free download pdf