In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.

Drawing shapes with Turtle in Python can be accomplished using a combination of the forward() and right() functions working together. The turtle direction tutorial has an example of how we can draw a square using these commands in Turtle. The example works, but it is not very elegant. We had to call each forward() and right() function a total of four times each in order to draw the square. This type of code repetition can be reduced by using loops in Python. In this tutorial, we’ll look at how to draw some shapes in Turtle using loops.


Square Code

Recall the original code to draw a square in Turtle that looked something like this. Notice the highlighted code which is simply the same two functions called several times in a row.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
left(90)
forward(150)
left(90)
forward(150)
left(90)
forward(150)
left(90)
forward(150)

done()

The for() loop

The same thing can be accomplished with this simple for() loop like so.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(4):
    left(90)
    forward(150)

done()

Both pieces of code produce the same result in Turtle.

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.


Experimenting With Loops

Loops are a fun way to simply start experimenting to see what the program will draw. Let’s try several examples with various iterations in the loops, and different values passed into the right() and forward() functions to see what happens.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(8):
    left(45)
    forward(75)

done()

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.


from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(16):
    right(100)
    forward(200)

done()

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.


from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(48):
    right(100)
    forward(127)
    left(50)

done()

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.


from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(48):
    right(100)
    forward(70 + i)
    left(50)

done()

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.


from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)

shape('square')
for i in range(75):
    right(20 + i)
    forward(1 + (i * 5))
    right(40 + i)

done()

In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.

As you can see, when incorporating loops into drawing with Turtle, you can come up with all kinds of interesting visual representations!

What type of loop structure repeats the code a specific amount of times?

A count-controlled loop repeats a specific number of times.

Is it possible to create a while loop that determines when the end of a file has been reached?

It is possible to create a while loop that determines when the end of a file has been reached. If the last line in a file is not terminated with /n the readline method will return the line without /n.

What is the structure that causes a statement or a set of statements to execute repeatedly?

A repetition structure causes a statement or set of statements to execute repeatedly. Repetition structures are used to perform the same task over and over. A condition-controlled loop uses a Boolean (true/false) condition to control the number of times that it repeats.

Which control structure would require you to create a diamond shape in a flowchart?

In a flowchart, both the decision structure and the repetition structure use the diamond symbol to represent the condition that is tested.