Edexcel GCSE Computer Science June 2024 Paper 2, Question 3: priced by count or by weight
Pearson Edexcel 1CP2/02 June 2024, Question 3: complete a Python program that prices a purchase by count or by weight, using constants, a logical operator, float input and validation of the type, the count and the weight. The ten marks explained, with the program to run.
Question 3 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 21 May 2024 (1CP2/02) is a 10 mark "complete the lines" question. Each gap is small: a constant, an operator, a float. The skill is reading the rest of the file to see which constant and which operator.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2024 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
Items are sold by count or by weight. The user enters 1 for a purchase by count or 5 for a purchase by weight, then the count or the weight in kilograms. Invalid input gets a message. Valid input gets the total cost.
The test data shows the prices: 3 items cost 3.69, so an item is 1.23. 4.5 kg costs 15.525, so a kilogram is 3.45. A count of 0, a weight of -6.6 and a purchase type of 3 are all invalid.
A finished program
PURCHASE_TYPE_ITEM = 1
PURCHASE_TYPE_WEIGHT = 5
PRICE_PER_ITEM = 1.23
PRICE_PER_KILO = 3.45
purchaseType = 0
totalCost = 0.0
purchaseType = int(input("Enter 1 for count or 5 for weight: "))
if purchaseType != PURCHASE_TYPE_ITEM and purchaseType != PURCHASE_TYPE_WEIGHT:
print("Invalid purchase type")
elif purchaseType == PURCHASE_TYPE_WEIGHT:
weight = float(input("Enter the weight in kilograms: "))
if weight > 0:
totalCost = weight * PRICE_PER_KILO
print("Total cost is", totalCost)
else:
print("Invalid weight")
else:
count = int(input("Enter the number of items: "))
if count <= 0:
print("Invalid number of items")
else:
totalCost = count * PRICE_PER_ITEM
print("Total cost is", totalCost)
Where the ten marks are
One each for: purchaseType = 0; the constant for a purchase by item; the logical operator and; the constant for a purchase by weight; float for the weight; * for the cost by weight; <= 0 for a bad count; > for a good weight; a print with a message and the total; and working with the test data.
Where the marks are lost
orin the type check. "Not 1 or not 5" is true for every number. The type is invalid when it is not 1 and not 5.- Typing 1 and 5. The gaps want the constants. That is what constants are for.
intfor the weight. 4.5 kilograms is a real number.< 0for the count. A count of 0 is invalid too, and it is in the test data.- A message with no total, or a total with no message. The last line needs both.
- Formatting the money. The paper says currency formatting is not required. 15.525 is the expected output.
Run it
Work through the five rows of the paper's test table.
The program
from bugbot import *
connect()
PURCHASE_TYPE_ITEM = 1
PURCHASE_TYPE_WEIGHT = 5
PRICE_PER_ITEM = 1.23
PRICE_PER_KILO = 3.45
purchaseType = 0
totalCost = 0.0
purchaseType = int(input("Enter 1 for count or 5 for weight: "))
if purchaseType != PURCHASE_TYPE_ITEM and purchaseType != PURCHASE_TYPE_WEIGHT:
led("red")
print("Invalid purchase type")
elif purchaseType == PURCHASE_TYPE_WEIGHT:
weight = float(input("Enter the weight in kilograms: "))
if weight > 0:
totalCost = weight * PRICE_PER_KILO
print("Total cost is", totalCost)
else:
led("red")
print("Invalid weight")
else:
count = int(input("Enter the number of items: "))
if count <= 0:
led("red")
print("Invalid number of items")
else:
totalCost = count * PRICE_PER_ITEM
print("Total cost is", totalCost)
Questions
Why use constants in a program?
A constant gives a fixed value a name, such as PRICE_PER_KILO. The code is easier to read, and if the value changes it only has to be changed in one place.
When is it AND and when is it OR in a validation check?
To test that a value is none of several allowed values, join the "not equal" tests with AND. To test that a value is one of several allowed values, join the "equal" tests with OR.
Why is 4.5 times 3.45 shown as 15.525 and not 15.53?
The question says currency formatting is not required, so the program prints the full result of the multiplication. Rounding it would be additional functionality.
More from this paper
- Question 2: Choose the right lines to make a Caesar cipher with ord() and chr() 10 marks
- Question 5: A menu loop with getChoice, getShape and addShape subprograms 15 marks
Every Edexcel 1CP2 question we have worked · Guide: Logic gates and truth tables explained
Learn it step by step
- F1.6 Variables, constants and assignment Programming basics
- F6.1 Defensive design and validation Robust programs
- F2.3 else, elif and Boolean operators Decisions and loops
This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.