For Loops
When we drew some simple shapes, we ended up repeating a lot of lines of code.
import turtle
wn = turtle.Screen()
bob = turtle.Turtle()
# square
bob.forward(100)
bob.left(90)
bob.forward(100)
bob.left(90)
bob.forward(100)
bob.left(90)
bob.forward(100)
bob.left(90)
wn.exitonclick()
Not only is this inefficient, it also increases the likelihood of typos and bugs. Thankfully, there is a way to tell our programs to repeat certain lines of code.
import turtle
wn = turtle.Screen()
bob = turtle.Turtle()
# square
for i in range(4):
bob.forward(100)
bob.left(90)
wn.exitonclick()
The line for i in range(4):
creates a “for-loop”, which will repeat the indented lines of code 4 times. Once the loop has finished, the program will continue on to execute the rest of the program.
How could we modify your program so it draws an equilateral triangle? a dodecahedron? an n-gon?
import turtle
wn = turtle.Screen()
bob = turtle.Turtle()
# n-gon
sides = 12
for i in range(sides):
bob.forward(100)
bob.left(360 / sides)
wn.exitonclick()
Now, modify your code so you ask the user to input 1) the number of sides, 2) background color, and 3) the pencolor.
import turtle
sides = input("Enter the number of sides: ")
background = input("Enter the background color: ")
pencolor = input("Enter the pencolor: ")
wn = turtle.Screen()
bob = turtle.Turtle()
wn.bgcolor(background)
bob.color(pencolor)
# n-gon
for i in range(sides):
bob.forward(100)
bob.left(360 / sides)
wn.exitonclick()
Take a look at this code:
for name in ["Brooklyn", "Eden", "Sophia", "Leif", "John", "Jesse"]:
print(name, "is a student in Computer Science.")
print()
This is called a for-loop in Python. It repeats the indented block of code multiple times, based on the first line. name
is called the loop variable, and it takes on each value in the list that follows it. For each value in the list, the for-loop executes the indented line of code. In this case, since there are six names in the list, the for-loop executes six times (or iterates), once for each value in the list.
There are other ways of creating a for-loop besides using a list, though. One of the most commonly used methods is to use the range()
function. Let’s explore this function in the interpreter
>>> for i in range(4):
print(i)
0
1
2
3
>>> for i in range(1, 4):
print(i)
1
2
3
range()
produces a set of numbers, starting a zero and going up to, but not including the stop-value. There must be at least one argument in the function, which is the stop-value: range(4)
. We can also include a start-value: range(2, 6)
.
The advantage of using range()
in our for-loops is that it allows us to quickly define how many times the loop should iterate:
>>>for i in range(3):
print("Hello, World!")
Hello, World!
Hello, World!
Hello, World!
>>>for i in range(1, 4):
print(i, "Hello, World!")
1 Hello, World!
2 Hello, World!
3 Hello, World!
Challenge: Polygon Problem
Now modify your n-gon program so it also asks the user for the fill color of a regular polygon. The program should draw the specified polygon and then fill it in.
Polygon Problem Solution
In order to solve this problem, you’ll need to know how to use the fillcolor()
method for turtles. Use this code to get you started:
import turtle
sides = input("Enter the number of sides: ")
background = input("Enter the background color: ")
pencolor = input("Enter the pencolor: ")
fillcolor = input("Enter the fillcolor: ")
wn = turtle.Screen()
bob = turtle.Turtle()
wn.bgcolor(background)
bob.color(pencolor)
bob.fillcolor(fillcolor)
# n-gon
bob.begin_fill()
for i in range(sides):
bob.forward(100)
bob.left(360 / sides)
bob.endfill()
wn.exitonclick()
Class Activity
Draw a square spiral using Turtle Graphics.
Group Problems
- Work Exercise 10.
- Work Exercise 13.
Useful Information
- Here is a partial list of supported color names.
- You can set the background color of a window, using something like
window.bgcolor("color-name")
. - You can let the user exit a window with the command
window.exitonclick()
. - The range function comes in three varieties:
range(stop)
andrange(start, stop)
andrange(start, stop, step)
. - Here is the complete turtle module documentation: https://docs.python.org/3/library/turtle.html#module-turtle.
HW:
- Read HTCS, 4.6-4.7
- Solve ex. 10 and start Project 3