Activity 2 - Simplifying code with loops

Great! You drew a square. But notice that we are writing the same lines over and over again! We had to repeat the instructions to draw a line and turn left 4 times. Wouldn’t it be great if we can give instructions once and tell the program to repeat it 4 times? Fear not, we can!

To do this, we need to use loops. Loops run a set of instructions multiple times. Lets use loops to creae a triangle. A triangle is a type of shape with three sides and three angles. Let’s build a triangle where the sides and the angles are the same. If you divide 360 degrees by the number of turns you need to make (which is 3 for a triangle), you’ll get the angle you need to turn.

So, 360 degrees ÷ 3 turns = 120 degrees per turn.

This means that each time you finish drawing a side of the triangle, you need to turn 120 degrees to be ready to draw the next side. By doing this three times, you’ll complete your triangle.

To see this in action, here is some example code that draws a triangle:

for i in range(3):
  turtle.forward(50)
  turtle.left(120)

Let’s understand what this code does. for i in range(3): This statement tells the program to run the instructions in a loop. range(3) sets a limit to how many times the loop runs (in this case, 3 times). The letter i is called a variable. It is used to keep track of how many times we have run the contents of the loop.

Tricky Python syntax - Part 1

Syntax is how we write languages so that it is understandable. In English we have spelling and grammar rules to help understand each other. Similarly, the Python language has syntax rules so that the computer can understand our code.

First, notice that at the end of the line with for, we added a colon (:); this signifies that the next line will be part of the for loop. The computer will complain if you miss the :!

As well, we added a tab in front of some lines of code in the example above. The tab tells the computer these statements are considered a part of the for loop.

To see how big of a difference this makes, take a look at the below code. Because we removed the tab in front of turtle.left(120), the code will no longer work as expected. Now, we only repeat turtle.forward(50) three times, and we get a straight line instead!

for i in range(3):
  turtle.forward(50)
turtle.left(120)

Now, take the example code that we have provided, and modify it slightly so that, instead of drawing a triangle, it draws a square. You can look back at what you did in Activity 1 as a hint.