Control Flow: The for
Loops
Drawing n-gons
We can modify our sample code to draw n-gons as regular polygons similar to squares such as a pentagon as in the following code:
import turtle
sides = 5
for _ in range(sides):
turtle.forward(100)
turtle.left(360/sides)
turtle.done()
In order to receive the number of sides of the n-gon, we can use the int()
and input()
functions as in:
import turtle
sides = int(input("Please input \"n\" for the n-gon"))
for _ in range(sides):
turtle.forward(100)
turtle.left(360/sides)
turtle.done()
Exercise: Draw a dashed line
Hint:
turtle.penup()
turtle.pendown()