2. Iteration

We have done some python, and we have used iteration (loops). Working in pairs, do the following exercises.

2.1. Big Question

  • What is iteration?
  • How can I use iteration to make my code smaller?

2.2. Keywords

  • Iteration
  • Loop
  • Indent
  • for i in range(n): where n is a number.

2.3. Iteration (Task 1)

    Iteration.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).
  • 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.

2.4. Iteration (Task 2)

    Iteration.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.

2.5. Iteration (Task 3)

Let us now look at a program. This one produces instructions for a person. The first version is in scratch.

_images/person-draw-triangle-scratch.png

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

_images/person-draw-triangle.png

Now in python. Run the program one line at a time, by pressing forward.

(iteration_2_3)

2.6. Iteration (Task 4)

    Iteration.4: How many times did the above code loop?
  • 1.
  • No, what is the number in the line
    for i in range(…):
  • 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(…):

2.7. Iteration (Task 5)

        Iteration.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()
        

2.8. Iteration (Task 6)

    Iteration.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, the number in
    for i in range(?):
    , and the angle determine the shape.

2.9. Iteration (Task 7)

    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

2.10. Iteration (Task 8)

        Iteration.8: Rearrange to make a program that draws a triangle.import turtle
---
for i in range (3):
---
    turtle.forward(50)
    turtle.left(120)
        

2.11. Iteration (Task 9)

        Iteration.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)
        

2.12. Iteration (Task 10)

        Iteration.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)
        

2.13. Iteration extra

Use the following area to experiment, try it out. I have started some code for you. What can you add or change? Do not do more than 10 lines (you can not save).

Next Section - 3. Challenges 1