Hardware Reference
In-Depth Information
The “range” Function
In the last few steps you used a list of numbers or integers and then colours to loop the
turtle sequence in Python. Using lists is a very common coding task, especially if you
are looping steps a number of times. It is so common that there is a Python function
that you can use instead, the range function.
A function is a piece of code that you can use over and over. You will learn more
about functions in Adventure 5.
In a new text editor window, type the following code and save it as FirstTurtle4.
py. When you have inished typing the code, select Run Run Module to see the code
in action (see Figure 4-13).
import turtle
alex = turtle.Turtle()
alex.shape(“turtle”)
for i in range(5):
alex.forward(100)
alex.left(72)
he range function in this program creates a list of numbers or integers in the same
way as the list you used before, [0,1,2,3,4,] .
DIGGING INTO THE CODE
The casing of commands used in Python code is very important, otherwise your
code will not work as expected and you may get errors. You will notice in the
examples in this adventure that most of the code is in lower case, except when
creating the “alex” turtle. The irst “turtle” in the line alex = turtle.
Turtle() is lower case ( t ), but the second one is upper case ( T ).
Search WWH ::




Custom Search