The worksheetDownload the PDF
Answers

F2.5 Count-controlled loops: for

Decisions and loops · GCSE · OCR J277 2.2.1, AQA 8525 3.2.2, Edexcel 1CP2 6.2.2 · about 15 min

BugBotLab

What this lesson is about

range, the loop variable, and repeating a known number of times.

Questions 9 marks in all

  1. [1 mark]What does this program print?

    for i in range(4):
        print(i)
    Answer:
    0
    1
    2
    3

    range(4) is 0, 1, 2, 3: four numbers, starting at 0 and never reaching 4.

  2. [1 mark]What does this program print?

    for n in range(1, 4):
        print(n, n * n)
    Answer:
    1 1
    2 4
    3 9

    range(1, 4) starts at 1 and stops before 4.

  3. [1 mark]What does this print? total = 0 then for i in range(5): total = total + i, then print(total)

    Answer: 10. 0 + 1 + 2 + 3 + 4 is 10.
  4. [1 mark]A square has four sides. Which loop drives it?

    1. Afor i in range(4): with forward and turn_right(30, angle=90) inside
    2. Bfor i in range(3): with forward and turn_right(30, angle=90) inside
    3. Cfor i in range(90): with forward inside
    4. Dfor i in 4: with forward and turn_right(30, angle=90) inside
    Answer: A. range(4) runs the block four times. for i in 4 is an error: a number is not something to loop over.
  5. [1 mark]How many times does the block under for i in range(10): run?

    Answer: 10. range(10) is 0 to 9: ten numbers.
  6. [1 mark]Put the lines in order to print corner 1, corner 2, corner 3.

    Number the lines 1 to 3 to put them in the right order.

    1. for i in range(3):
    2. print("corner", corner)
    3. corner = i + 1
    Answer:
    for i in range(3):
        corner = i + 1
        print("corner", corner)

    The for line comes first, and the lines of the block are indented under it.

  7. [1 mark]What does this program print?

    for n in range(0, 20, 5):
        print(n)
    Answer:
    0
    5
    10
    15

    The third number is the step: 0, 5, 10, 15, stopping before 20.

  8. [1 mark]In OCR's Reference Language, for i = 1 to 5 ... next i. How many times does the loop run?

    Answer: 5. The Reference Language includes the end number: 1, 2, 3, 4, 5. Python's range(1, 5) would stop at 4.
  9. [1 mark]What does AQA call a count-controlled loop?

    1. ADefinite iteration
    2. BIndefinite iteration
    3. CSelection
    4. DRecursion
    Answer: A. Definite: the number of times is known before it starts.

The task: count the corners

Drive a square of 30 cm sides with a for loop, printing corner 1, corner 2, corner 3, corner 4 as you go, and finish facing the way you started.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

for i in range(4):
    forward(60, distance=30)

The hint students can ask for: One trip round a square is the same two steps repeated four times, so put them in a loop and use the loop's counter in what you print.

A solution

from bugbot import *
connect()
for i in range(4):
    forward(60, distance=30)
    turn_right(30, angle=90)
    print('corner', i + 1)

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.