AQA GCSE Computer Science June 2023 Paper 1, Question 15: the split the bill program
AQA 8525 June 2023 Paper 1, Question 15: write a Python program that takes payments towards a restaurant bill until it is paid, then reports the tip. A model answer built step by step, the eight marks explained, and the program to run.
Question 15 of the AQA GCSE Computer Science Paper 1 sat on 19 May 2023 (8525/1B, the Python paper) is an 8 mark "write a program" question. It needs a loop that you cannot count in advance, so it is a test of while.
We do not copy the exam paper here. Open it beside this page: AQA June 2023 Paper 1B question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A group shares a restaurant bill. Each person pays what they like. The program must:
- get the total amount of the bill;
- get a payment from a person, and subtract it from what is left;
- if there is still something left to pay, output how much, and ask the next person;
- if what is left is exactly 0, output
Bill paid; - if what is left is less than 0, output
Tip isand the size of the overpayment.
Plan before you write
Ask yourself three questions about any loop question.
What repeats? Getting a payment and subtracting it. That goes inside the loop. Getting the total happens once, so it goes before the loop.
When does it stop? When the amount left is 0 or less. So it carries on while left > 0.
What happens afterwards? One of two messages. That is an if and an else after the loop.
A model answer
left = float(input("Total bill: "))
while left > 0:
payment = float(input("How much are you paying? "))
left = left - payment
if left > 0:
print("Left to pay:", left)
if left == 0:
print("Bill paid")
else:
print("Tip is", -left)
The tip is -left because left has gone negative. If the bill had 4 left and someone paid 10, left is -6 and the tip is 6.
Money is not always a whole number, so the inputs use float. int would still get most of the marks.
Where the eight marks are
Three marks are for design:
- storing an input in a variable with a meaningful name;
- using a loop that tries to pay the bill off;
- using selection with an
elseorelif, or more than oneif.
Five marks are for a program that works:
- getting the total outside the loop and subtracting a payment inside it;
- a loop that stops, in every case, once the bill is paid;
- two conditions: one for the bill being paid, one for there being a tip;
- outputting
Tip isand the tip as a number, and only when money is left over; - outputting
Bill paid, and the amount left to pay, in sensible places.
Any error in the code caps you at 7.
Where the marks are lost
- Asking for the total inside the loop. Then every person resets the bill and it is never paid.
while left != 0. If someone overpays,leftjumps from positive to negative without ever being 0, and the loop runs for ever.> 0stops in both cases.- Outputting the tip as a negative number. "Tip is -6" is wrong. Subtract from 0, or use
-left. - Printing
Tip is 0. When the bill is paid exactly there is no tip. The two messages need separate conditions. - A
forloop. You do not know how many people will pay, so you cannot count the passes. If the number of repeats is unknown, it is awhile.
A note on pennies
Try a bill of 0.3 paid with 0.1 three times and Python says there is a tiny amount still to pay. Decimal fractions are not stored exactly in binary. Rounding after each subtraction fixes it: left = round(left - payment, 2). The mark scheme accepts answers with or without it.
Run it
Type the bill, then payments, when it asks. The robot's light is red while money is owed and green when the bill is settled.
The program
from bugbot import *
connect()
left = float(input("Total bill: "))
led("red")
while left > 0:
payment = float(input("How much are you paying? "))
left = round(left - payment, 2)
if left > 0:
print("Left to pay:", left)
led("green")
if left == 0:
print("Bill paid")
else:
print("Tip is", -left)
Questions
What is the answer to AQA GCSE Computer Science 2023 Paper 1 Question 15?
Read the total once, then loop while the amount left is greater than 0: read a payment, subtract it, and output what is left if it is still above 0. After the loop, output Bill paid if the amount left is 0, and otherwise output Tip is and the amount left made positive.
When should I use a while loop and not a for loop?
Use a for loop when you know how many times to repeat before you start. Use a while loop when the repeating depends on something that changes inside the loop, such as a bill being paid off or a user entering valid data.
Why does my program say there is money left when the bill has been paid?
Decimal fractions such as 0.1 cannot be stored exactly in binary, so repeated subtraction can leave a tiny remainder. Round the result to 2 decimal places after each subtraction.
More from this paper
- Question 7: A ticket price with a group discount 6 marks
- Question 12.2: Write your own linear search 7 marks
- Question 13: Validate a grid reference until it is correct 6 marks
- Question 16: A dice game to 21 with random numbers 11 marks
Every AQA 8525 question we have worked · Guide: Logic gates and truth tables explained
Learn it step by step
- F2.6 Condition-controlled loops: while Decisions and loops
- F2.3 else, elif and Boolean operators Decisions and loops
- F13.4 Programming questions Exam preparation
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.