Lesson 3

Incorporating Loops


A for loop is a special kind of loop in python, that runs a certain number of times, and then stops. We can make a while loop work like it was a for loop, but a for loop saves us a few lines of code in an elegant way

On line 18 add the following code for length in range(200):

This loop will run once for every length in the range> between 0 and 200. For loops always start at zero when you write them this way.

The word length is actually a variable that we can use throughout the loop, and it will increase from zero all the way up until 199, one before the end of the range. In the next section we will take advantage of this fact.

Building on Loops


Part of what makes for loops so useful is the fact that we can use the variable that we are counting with to do cool things.

Let’s try using some of our turtle functions, and see what happens when we pass “length” as a parameter

On line 19, add the following code t.forward(length)

On line 20, add the following code t.right(90)

it will move forward increasingly far, but always turn afterwards at a 90 degree angle.