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

(vip2019) #1
Loops Are Fun (You Can Say That Again) 57

4, and 5 a total of four times, generating a rosette of four circles
on the top, left, bottom, and right sides of our window. Let’s take
a step-by-step look through the loop as it draws our rosette, one
circle at a time.



  1. The first time through the loop,
    our counter x has a starting value
    of 0 , the first value in the range
    list [0, 1, 2, 3]. We draw our first
    circle at the top of the window with
    t.circle(100) and then turn the
    turtle to the left by 90 degrees with
    t.left(90).

  2. Python goes back to the beginning
    of the loop and sets x to 1 , the sec-
    ond value in [0, 1, 2, 3]. Then it
    draws the second circle on the left
    side of the window and turns the
    turtle left by 90 degrees.

  3. Python goes back through the loop
    again, increasing x to 2. It draws the
    third circle at the bottom of the win-
    dow and turns the turtle left.

  4. On the fourth and final time through
    the loop, Python increases x to 3 , then
    runs t.circle(100) and t.left(90) to
    draw our fourth circle on the right side
    of the window and turn the turtle. The
    rosette is now complete.

Free download pdf