> For the complete documentation index, see [llms.txt](https://code-for-life.gitbook.io/python-den/DGiFT28ihVJK7ghZQHqu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-for-life.gitbook.io/python-den/DGiFT28ihVJK7ghZQHqu/python-6-iteration-part-2.md).

# Python 6: Iteration Part 2

Teaching Resource

{% tabs %}
{% tab title="Resources" %}
Slides **Python 6: Iteration 2**

* You will need to either have Python IDLE installed or have access to an online Python IDE. We have used <https://editor.raspberrypi.org/en>
* Activity worksheets are included in this lesson. You will need to distribute these to your pupils
* We have added a walk-through video below
  {% endtab %}

{% tab title="Prior Knowledge" %}
Printing, mathematical operators, data types and input using Python

* Selection in Python using `if..elif..else`
* counted `while` loops in Python&#x20;
  {% endtab %}
  {% endtabs %}

{% file src="/files/UyeapuqHLfmLDeuusJut" %}

#### Please test out our [assessments here](https://code-for-life.gitbook.io/code-for-life-test-space/)  <a href="#learning-objectives" id="learning-objectives"></a>

## **Lesson walk-through**

[Watch on YouTube](https://youtu.be/Xhibd1JxPrU)

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwAuC4Q5WQz4ea2O2b2JP%2Fuploads%2FAejhabL6uQNcH267vHVP%2FPython%20Den%20Session%206_%20Iteration%2C%20part%202.mp4?alt=media&token=481bcf65-399f-46fa-afac-32b1457fb9de>" %}
Session 6: Iteration, part 2
{% endembed %}

## **Learning Objectives**

&#x20;To teach pupils to:

* use what they have learned about `while` loops so far in Python exercises outside Rapid Router
* to write more complex programs by using prior solutions as scaffolds
* to repeat input in a loop for totalling
* to  begin to use selection inside a loop

## Slide Notes

### Starter

**Slide 2:** Show the six questions on the board and ask pupils to write down their answers. They should work on their own and do their best to answer all questions, even if they are not sure.&#x20;

**Slide 3:** Show the answers and ask pupils to correct their own answers. If they are going to keep the sheet, ask them to mark and correct in a different colour so it is obvious what they did know and what they did not.&#x20;

### Activity 1: Investigation

**Slide 5:** This slide shows the solution to level 122. Discuss this with pupils and ensure they understand why it is fine to re-use the `count` variable - you might find that some used `count1`, `count2` and `count3.` There is no need to introduce unnecessary variables into the solution.

For the rest of this lesson, we will practise Python while loops outside Rapid Router.&#x20;

Show **slide 6** and ask the pupils how we can use what we did before to answer this new question. The expected output of this new program is:

```python
0
1
2
3
4
5
6
7
8
9
10
Finished
```

**Slide 7** identifies the code that is repeated. We need to replace this with a print statement but keep the counting there.&#x20;

**Slide 8:** On this slide, we change the loop so that it repeats ten times instead of four times. Then we add the `print` statement. Ask pupils where the `print("Finished")` should go. This should only be output once.&#x20;

**Slide 9** shows the correct placement for this line of code. Ensure that pupils understand this. You might like to ask pupils what would happen if it was indented instead. If pupils find this confusing, demonstrate it yourself. E.g.&#x20;

<figure><img src="https://4077022412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwAuC4Q5WQz4ea2O2b2JP%2Fuploads%2FDzDqEKWR6WQGgmGJ5zfS%2Fimage.png?alt=media&amp;token=9068b3c6-44fb-49df-b139-045d090673ec" alt=""><figcaption><p>Example 1: print("Finished!") is not in the loop so it is not repeated.</p></figcaption></figure>

<figure><img src="https://4077022412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwAuC4Q5WQz4ea2O2b2JP%2Fuploads%2FGnpnFfKbkYo5mgboPufX%2Fimage.png?alt=media&amp;token=3ab9736c-5f29-4578-ac25-306deea0ff2e" alt=""><figcaption><p>Example 2: print ("Finished!") is in the loop so it is repeated each time</p></figcaption></figure>

### Activity 2: Exploring the while loop

**Slide 11:** Once you have explored this code, ask pupils to do the **Activity 1: Using Python** exercises on the worksheet. The solutions follow on **slides 12–14**. Avoid showing these solutions until you have checked pupils' work.&#x20;

### Activity 3: Input and Totalling with the `while` loop

**Slides 15–18** start to explore how we can use what we have done to ask the user to enter five numbers and output their total. Work through these slides, asking pupils for answers to the questions posed. Pupils should try the code on slide 18.

**Slides 19–24** explore how they can further extend this code to total up these numbers. Once you have worked through the slides, ask the pupils to update their programs and test them. If they get stuck, ask them to carefully compare their code to the example given.&#x20;

**Slides 25–26** explore some common errors with loops. See if pupils can spot the problems. The output is there to help them.&#x20;

**Slide 27:** Once pupils are feeling confident with this work, ask them to do the exercises. The answers follow on slides **28–30**. Again, check pupils' work before showing these answers.&#x20;

### Plenary

**Slide 31:** The exit ticket shows some Python code with five errors. Ask pupils to work in pairs to find the errors.&#x20;

The errors are:

* `count` has not been initialised before the loop
* `total = 0` must come before the loop&#x20;
* You must ask for the number to be input before you can add it to the total
* You must store `count + 1` back into the `count` variable so it changes
* The final `print` statement should output `total` and not `number`
