Session 3

• To introduce loops in Python

• To look at for loops

• To introduce indeterminate loops

Introduction (10 mins)

  • Use slides 2 - 3: Code comparison Show the repeat block alongside the Python code and relate each block.

  • Slide 5: Compare the block code on the left to the Python code. Ask participants what they notice about the Python code. The important things to note are:

    • coloured keywords: from, import, van, in

    • round brackets after range

    • colon after the closing bracket of range

    • the repeated code is indented (moved right usually four spaces). Indentation is very important in Python. It is the only thing that shows what code is inside a code block. In this example, the code block is the for loop.

    • Participants should do level 83 and look carefully at the Python code.

Nested loops (10 mins)

  • Slide 8 - 12: Nested loops : Ask participants to examine the code on slide 8 and work out how many times the right turn is repeated. On slide 9, emphasise the indentation. It is only the indentation that indicates which code is inside which loop. Ask questions about this, e.g. what would happen if the final line was not indented at all/ indented one more time etc. The following slides explore this.

  • Participants should do level 84

Indeterminate loops (15 mins)

  • Note the difference between the counted for loop and this indeterminate while loop. With the counted loop, we had to specify how many times the loop should be repeated. With an indeterminate loop, the code inside the loops keeps being repeated until some condition is met.

  • Slide 13 - 18 while loops: Encourage pupils to study the Python code being created on these levels.

  • Participants do levels 87 - 89

Quiz (10 mins)

  • Slide 19: Answer: It will go forward because that is the first question asked. Note that the ELIF makes the conditions mutually exclusive - they only get to pick one outcome so the code will move the van right and then forward and that is all.

  • Slide 20: Answer: No, this is inefficient because the first question asked is if there is a left turn. There are no left turns at all so this is a wasted question. If we got rid of that, it would still be inefficient because there are many more forwards than rights. The code should ask if there is a forward first and then use an else for the right.

  • Slides 21 - 22: Note that the ELIF makes the conditions mutually exclusive - they only get to pick one outcome so the code will move the van right and then forward and that is all.

Summary (5 mins)

  • Wrap the lesson up by asking participants what they can recall about the key points: indentation, the use of nesting and mutually exclusive conditions using elif.

Homework

  • If possible, participants should complete all levels up to level 91, including any they might not have finished during the session.

  • They should look carefully at the Python code produced as they will be writing this themselves in the next session.

Last updated