For Loops For Turtle
For Loops For Turtle
The letter ‘i’ is quite commonly used for the loop variable of a
loop (‘i’ for ‘index’), although you can use any variable name
Drawing a Star Shape Using a For Loop
• You can alter the program to draw a star shape
• This for loop runs five times to create the five
lines of the star:
for i in range(5):
turtle.forward(400)
turtle.right(144)
turtle.backward(size * (i + 2))
Move the turtle to turtle.right(90)
the starting point turtle.forward(size)
of the next row turtle.left(90)
Drawing the Rows of Dots
for i in range(0, 15, 2):
for j in range( i + 1 ):
...
• As you can see from the loops, the inner loop runs a
number of times based on the value of the outer loop
– The first time the inner loop runs, it draws 1 dot
– The second time it runs, it draws 3 dots
…
– The last time it runs, it draws 15 dots