2. Iteration (e)¶
We have done some python, and we have used iteration (loops). Working in pairs, do the following exercises.
-
Iteration-2.1: Click to two correct answers. Why would you use a loop, in your code?
- It will make it easier to change the code later.
- Yes, because there is less code, it will be easier to change.
- To make a decision.
- No, you would use a selection (if), to do that (we have not done them yet).
- I will not have to type as much.
- Yes, because there is less code, you will not have to type as much.
- To calculate the value of an angle.
- No, it is not used to calculate.
-
Iteration-2.2: What does the program do when it finishes looping?
- The program stops.
- No, the program stops when there is no more code to run.
- It runs the line of code after the loop.
- Yes, it does what ever is next.
- It repeats the loop.
- No, read the question carefully. It has finished looping, so it has already done this.
Let us now look at a program. This one produces instructions for a person. The first version is in scratch.

Then this version is in a sort of pythony scratch. It looks like scratch, but is python.

Now in python. Run the program one line at a time, by pressing forward.
(iteration_e2_3)
-
Iteration-2.4: How many times did the above code loop?
- 2
- No, what is the number in the line
for i in range(…):
- 3
- Yes, because we have the number 4 in the line
for i in range(4):
- 4
- No, what is the number in the line
for i in range(…):
Iteration-2.5: Drag the blocks to the right, to make a program that draws a square, then draws a triangle.import turtle
---
for i in range (4):
---
turtle.forward(50)
turtle.left(90)
---
draw_triangle()
-
Iteration-2.6: Why did two lines of code need to be indented (moved away from the left edge)?
- Because they are turtle commands.
- No, look again. Which commands are run multiple times.
- Because there is two of them.
- No, there can be one or more commands, that are indented.
- Because these are the commands to be repeated.
- Yes, we repeat the indented commands.
-
Iteration-2.7 Drag the sentence endings to the sentence starts on the right.
Have you done the ones that you know? Have you discussed with another pair?
- indented.
- The repeated code is
- the line after the indentation is run.
- After the loop is finished
- ends with a colon:
- The line
for i in …
- the space at the beginning of the line.
- Indentation is
Iteration-2.8: Rearrange to make a program that draws a triangle.import turtle
---
for i in range (3):
---
turtle.forward(50)
turtle.left(120)
Iteration-2.9: Rearrange to make a program that draws a triangle, then a square.import turtle
for i in range (3):
---
turtle.forward(50)
turtle.left(120)
---
for i in range (4):
turtle.forward(50)
turtle.left(90)
Iteration-2.10: Rearrange to make a program that draws 4 trianglesimport turtle
---
for i in range (4):
---
for i in range (3):
turtle.forward(50)
turtle.left(120)
---
turtle.left(90)
Use the following area to experiment, to help with the previous question. Then paste the solution into idle.