AQA GCSE Computer Science sample Paper 1, Question 9: the robot in the grid

AQA 8525 sample assessment material, Paper 1 Question 9: follow programs built from Forward, TurnLeft, TurnRight and ObjectAhead to draw a robot's path, including a WHILE loop that feels its way round objects. Worked step by step, with the real robot doing it.

Past paper questionAQA 8525/1BSample Paper 16 marksRead the code

Question 9 of AQA's sample assessment material for GCSE Computer Science Paper 1 (8525/1B, the Python paper) gives you four robot subroutines and two short programs, and asks you to draw where the robot goes. 3 marks each. A robot is the one thing this site can actually run, so this page ends with a real one.

We do not copy the paper here. Open it beside this page: AQA 8525 Paper 1B sample question paper (PDF). When you have finished, check the sample mark scheme too.

The subroutines

  • Forward(n) moves the robot n squares forward.
  • TurnLeft() and TurnRight() turn it 90 degrees on the spot.
  • ObjectAhead() returns true if the next square has an object in it.

The robot starts facing up the grid.

Part 1: a straight sequence

Forward(2), TurnLeft(), Forward(1), TurnRight(), Forward(1).

Up two squares. Turn to face left. One square left. Turn to face up again. One square up. The path is two squares up, one to the left, one up: an L with a step.

One mark for the first two squares, one for the square to the left, one for the last square up.

Part 2: feeling round objects

The program loops while there is an object ahead: turn left; if there is now an object ahead as well, turn right twice (so the robot faces the other way); then go forward one. After the loop, go forward one more.

In words: every time the robot is blocked, it tries to go left round the object, and if left is blocked too it goes right. When nothing is ahead, the loop ends and it takes one last step forward.

Trace it against the grid on the paper square by square, asking "is the next square black?" at each ObjectAhead(). Three squares are marked, and each is a mark.

Where the marks are lost

  • Turning the robot's left and your left. The robot's left depends on which way it is facing. After a TurnLeft() from facing up, it faces left. After TurnLeft() from facing left, it faces down.
  • Moving on a turn. TurnLeft() and TurnRight() do not change the square.
  • Forgetting the last Forward(1) after the loop in part 2.
  • Stopping the loop too early. It only ends when ObjectAhead() is false at the top of the loop.

Run it

The first program on a real robot, with each square 10 cm. Then a version of the second program: the robot drives until its distance sensor sees something ahead, turns left, and carries on. Watch the overhead view.

Two squares up, one left, one up, then a pause. Then it drives until something is ahead and turns left round it.
The program
from bugbot import *
connect()

SQUARE = 10

def Forward(n):
    forward(60, distance=n * SQUARE)

def TurnLeft():
    turn_left(30, angle=90)

def TurnRight():
    turn_right(30, angle=90)

def ObjectAhead():
    return distance() < SQUARE

# part 1
Forward(2)
TurnLeft()
Forward(1)
TurnRight()
Forward(1)
wait(1)

# part 2, on whatever is in front of the robot now
while ObjectAhead():
    TurnLeft()
    if ObjectAhead():
        TurnRight()
        TurnRight()
    Forward(1)
Forward(1)
print("done at", position())
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to AQA sample Paper 1 Question 9.1?

The robot moves two squares up, one square to the left, then one square up.

What does the loop in Question 9.2 do?

While something is directly ahead, the robot turns left; if that way is blocked too, it turns to face the opposite way instead. Then it moves one square. When nothing is ahead it moves one final square.

How do I trace a robot program?

Keep track of two things at every step: which square the robot is in and which way it is facing. Turns change only the facing. Forward changes only the square, in the direction it is facing.

More from this paper

Every AQA 8525 question we have worked

Learn it step by step

  1. F4.1 Writing functions Functions and structured code
  2. F2.6 Condition-controlled loops: while Decisions and loops
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by AQA, and the question paper and mark scheme remain AQA's copyright. Read them on AQA's site with the links on this page.