Python and Geometry
Now we are going to introduce a new tool to help us integrate the syntax we have learned with geometry. To do this we will use ‘turtle’, a module that lets us draw by moving a turtle around.
Simple Square
Turtle lets you move, turn, set pen color, and many other things. Let’s start with an exercise: draw a square!
import turtle
t = turtle.Pen()
t.pendown()
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
Now to Loop
Can anyone figure out how to simplify the above code? Usually someone figures it out pretty quickly that we can use for loops.
import turtle
t = turtle.Pen()
for i in range(4):
t.forward(100)
t.left(90)
Coding Challenge: N-Sided Shapes
Most students should know how to calculate the interior angle of a polygon. The coding challenge is to implement a program that asks the user how many sides to draw on a shape, then draw it.
Advanced Challenge
Once they have done the above have them experiment some. What happens if you loop drawing the shape over and over again at slight angles? What other cool patterns can the invent?