OCR GCSE Computer Science June 2022 Paper 2, Question 5(e): the car park algorithm

OCR J277/02 June 2022, Question 5(e): write an algorithm that charges 4 pounds an hour, or 2 for an electric car, and repeats until the user enters 0 hours. Model answers in OCR reference language and Python, the six marks explained, and the program to run.

Past paper questionOCR J277/02June 2022 Paper 26 marksWrite a program

Question 5(e) is the last question of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02): 6 marks for an algorithm in OCR Exam Reference Language or a high-level language. It is a selection inside a loop that ends on a sentinel value.

We do not copy the exam paper here. Open it beside this page: OCR June 2022 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A car park charges £4 an hour. For an electric car the price is halved, to £2 an hour. Write an algorithm to:

  • take as input the number of hours parked, and whether the car is electric;
  • calculate and output the total price;
  • repeat continually until the user enters 0 hours.

A model answer

In OCR Exam Reference Language:

hours = int(input("Enter hours parked, or 0 to stop"))
while hours != 0
    electric = input("Is the car electric? Y or N")
    if electric == "Y" then
        price = hours * 2
    else
        price = hours * 4
    endif
    print(price)
    hours = int(input("Enter hours parked, or 0 to stop"))
endwhile

In Python:

hours = int(input("Enter hours parked, or 0 to stop: "))
while hours != 0:
    electric = input("Is the car electric? Y or N: ")
    if electric == "Y":
        price = hours * 2
    else:
        price = hours * 4
    print(price)
    hours = int(input("Enter hours parked, or 0 to stop: "))

Reading the hours once before the loop and again at the bottom means that a 0 ends the loop at once, without asking a pointless question about a car that is not there. The mark scheme also accepts both inputs at the top of the loop, and does not mind a 0 being output as the loop exits.

Where the six marks are

  • inputting the hours and whether the car is electric, as two separate inputs;
  • checking whether the car is electric;
  • the right price for an electric car (hours times 2), output;
  • the right price for any other car (hours times 4), output;
  • an attempt to repeat all of that;
  • repeating until 0 hours is entered.

Where the marks are lost

  • while hours > 0. The mark scheme refuses it. The question says the loop ends when the user enters 0, and -1 is not 0.
  • Starting hours at 0. If you initialise hours = 0 and then write while hours != 0, the loop never starts.
  • One input for both things. The hours and the electric answer are two values and need two inputs.
  • Halving twice. Either charge 2 an hour for an electric car, or work out the price at 4 and halve it. Not both.
  • A for loop. Nobody knows how many cars will come. It ends on a value, so it is a while.

Run it

Type hours and Y or N for each car, then 0 to stop. The robot's light is green for an electric car.

Try 3 and N for 12, then 3 and Y for 6, then 0 to stop.
The program
from bugbot import *
connect()

hours = int(input("Enter hours parked, or 0 to stop: "))
while hours != 0:
    electric = input("Is the car electric? Y or N: ")
    if electric == "Y":
        led("green")
        price = hours * 2
    else:
        led("white")
        price = hours * 4
    print(price)
    hours = int(input("Enter hours parked, or 0 to stop: "))
led("off")
print("Car park closed")
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 OCR J277 June 2022 Paper 2 Question 5(e)?

Input the hours. While hours is not 0: input whether the car is electric, set the price to hours times 2 if it is and hours times 4 if not, output the price, and input the hours again.

How do I make a loop repeat until the user enters 0?

Use a condition-controlled loop with the condition hours != 0. Read the value before the loop so that the condition can be tested, and read it again at the end of each pass.

Why is a while loop used and not a for loop?

A for loop repeats a known number of times. Here the number of cars is not known in advance. The loop ends when a particular value is entered, so it must be condition-controlled.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F2.6 Condition-controlled loops: while Decisions and loops
  2. F2.2 Selection: if Decisions and loops
  3. F13.4 Programming questions Exam preparation
Open the lessons

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