Hardware Reference
In-Depth Information
By using a loop to repeat a sequence of code, you have saved yourself a number of lines
of code. By using iteration in your program you are thinking like a computer scientist.
Lists can contain more than numbers or integers. For example, they can contain infor-
mation to change the colour of the turtle pen.
Amend your Python pentagon code to look like the following, making sure to include
the letter a before color :
import turtle
alex = turtle.Turtle()
alex.shape(“turtle”)
for aColor in [“red”, “blue”, “yellow”, “green”, “purple”]:
alex.color(aColor)
alex.forward(100)
alex.left(72)
Save the ile as FirstTurtle3.py and run the module. You will now have a more
colourful pentagon shape, as shown in Figure 4-12.
FIGURE 4-12 Using loops and lists to create turtle shapes in Python
Search WWH ::




Custom Search