4. Iteration (h)¶
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?
- To add up all of the values in a list.
- No, while it could be used in a program that does that, its job is not calculation.
- 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).
- It makes the code easier/quicker to read.
- Yes, the code is easier to read, because you can easily see that it does the same thing each time.
- 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 starts the program again.
- No, it would only start again, if you tell it to.
- It repeats the loop.
- No, read the question carefully. It has finished looping, so it has already done this.
- It runs the line of code before the loop.
- No, the program runs from top to bottom.
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_h2_3)
-
Iteration-2.4: How many times did the above code loop?
- 120
- No, what is the number in the line
for i in range(…):
- 2
- No, Look at the numbers.
- 3
- Yes, because we have the number 4 in the line
for i in range(4):
- 4
- No, Look at the numbers.
- 50
- 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.
- Because squares have 4 sides.
- No, this is a true statement, but not the answer to this question.
- We indent to the right, because we are turning left.
- No, look again. Which commands are run multiple times.
-
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 …
- the space at the beginning of the line.
- Indentation is
- optional, and should be used to make the code more readable.
- Blank lines are
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.