In Lab 2, we drew our first shape, a square, in using the turtle module. Your code probably looked something similar to this:
This function draws a square of size n using Turtle graphics
You probably noticed that the following two lines were repeated four (4) times:
I would be much easier if we could tell the computer to do this four times instead of copying and pasting the same lines over and over again. Luckily, we can. This is the idea of iteration. We can either define how long we want to repeat a set of instructions or we can define when we should stop repeating those sets of instructions.
The FOR Loop
A for loop should be used when you know the exact number of times the loop should run. For instance, do this nnumber of times. A for loop consists of two parts: an iterator and the object to be iterated.
for (iterator) in (iterable thing) :
# Your block of code here
One common function, technically a built-in data type, is called range. Range creates a data type similar to a list, more on that on Thursday, of integers. Similar to string indexing, range will create a set of numbers from the starting number up to, but not including, the last number. For instance, calling
range(0,4)will create a iterable object of 0, 1, 2, 3. Now, in a for-loop, the iterator will take on the value of 0, then 1, then 2, then 3. After it reaches the end of the iterable object, it will stop.
for iter in range(0,4):
print("iter =", iter)
Sometimes, we use the value of the iterator. In this case, we have printed the iterator. Other times, it may simply act as a counter to keep track of how many times the loop has run. For instance, the following loop does use the value of the iterator. It simply keeps track of how many times the loop has run.
Drawing a Square using a For-Loop
Let’s look at a second example that draws a flower using Turtle graphics.
New Module: Random
Now, we will introduce a new module in Python, random. Random allows for generating pseudo-random numbers, shuffling, and other related tasks. For this example, we want the function randint(start, stop) that generates random numbers between and including both endpoints.
This code produces an random integer between 0 and 255.
Circling back to our previous example, we will now draw a flower, but color in the pedals using random numbers. First we must set the color in turtle using the function
fillcolor(). We will then tell it when to start filling and when to stop filling. For each pedal we generate a new set of random numbers.
The WHILE Loop
Sometimes the number of iterations the loop will perform is not readily known. We want to test the case ad run the loop if the test case is True. Otherwise, we will not run the loop. Here is the syntax for a while-loop.
while ( logical statement == True):
# Your block of code here
Next, let’s create a game that generates a random number and the user must guess the random number. We can again call on our
randint()function from the random module. The user continually guesses a number until the correct number is reached.
This next section of code produces the same results as the above line; however, instead of checking to see if the value is correct in the loop, we check to see if a Boolean variable is true. If it is not, we continue running the loop. Look at this example and see how it compares to the previous example!