Session 4, #1: Drawing Shapes with Loops


A loop allows you to repeat commands multiple times. For example, this program will draw a square:

Let's examine what each line does in the program:

1 import turtle Directs Python to load the turtle package, so, we can use the commands in it.
2 Blank lines are ignored by Python.
3 ty = turtle.Turtle() Creates a variable called ty. Variables store information while our program is running. ty keeps track of the location and direction facing.
4 ty.color("orange") Change ty's color to be orange.
5 Blank lines are ignored by Python.
6 for i in range(4): The for loop repeats the indented statements below it.
7   ty.forward(100) Move ty forward 100 steps.
8   ty.left(90) Turn ty 90 degrees to the left.

By changing the range(), we change the number of times the loop repeats.

Hexagon Challenge




Challenge: • Modify the program to make a hexagon (6 sided polygon).