Edexcel GCSE Computer Science June 2023 Paper 2, Question 4: the volume of a prism

Pearson Edexcel 1CP2/02 June 2023, Question 4: write a Python program that takes three decimal inputs, rejects any that are not above zero, and displays the area of a triangle rounded to 2 places and the volume of a prism in 8 columns with format(). A model answer and the fifteen marks.

Past paper questionPearson 1CP2/02June 2023 Paper 215 marksWrite a program

Question 4 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 25 May 2023 (1CP2/02) is a 15 mark program. The maths is two lines. The marks are in the validation and in formatting the output exactly as asked.

We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2023 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

The user enters the base and height of a triangle, and the length of a prism, as decimal numbers. All three must be greater than zero.

  • If any input is invalid, display an error message and do not process it.
  • Otherwise display the area of the triangle (half of base times height) rounded to two decimal places.
  • Display the volume (area times length) using <string>.format(), in eight columns with two decimal places, followed by the words cubic units.
  • In every case, display a goodbye message just before the program ends.

A model answer

base = float(input("Enter the width of the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
length = float(input("Enter the length of the prism: "))

if base <= 0.0 or height <= 0.0 or length <= 0.0:
    print("Invalid input")
else:
    area = 0.5 * base * height
    print("Area of the triangle is", round(area, 2))
    volume = area * length
    print("Volume is {:<8.2f} cubic units".format(volume))

print("Goodbye")

Reading the format string

{:<8.2f} has four parts. < means line the number up on the left. 8 is the width in columns. .2 is two decimal places. f means a fixed-point number. So 250.0 becomes 250.00 followed by two spaces, to make eight columns.

Where the fifteen marks are

Twelve single marks: three inputs; converted to real numbers with float; a relational operator checking for invalid input; a logical operator joining the checks; an error message; the area formula; the area rounded to 2 places; the volume formula; a string formatting function for the volume; the 8 columns and 2 decimal places; the goodbye message shown in all cases; and meaningful variable names.

Then up to 3 for how well it runs with the test data, and with negative numbers.

Where the marks are lost

  • and between the checks. Invalid means any of the three is wrong, so it is or.
  • round() for the volume. The mark scheme says rounding is not string formatting. The question names format().
  • Goodbye inside the else. Then invalid input gets no goodbye. It goes after the if, with no indentation.
  • int(input()). The inputs are decimal numbers.
  • 1/2 * base * height written as 1 / (2 * base * height). Check your answer against the paper's test data: 4.567, 1.23 and 89.01 give an area of 2.81 and a volume of 250.00.

Run it

Use the two prisms from the paper. The robot drives 1 cm for every 10 cubic units.

4.567, 1.23 and 89.01 give an area of 2.81 and a volume of 250.00. Try 0 for the base: Invalid input, and still Goodbye.
The program
from bugbot import *
connect()

base = float(input("Enter the width of the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
length = float(input("Enter the length of the prism: "))

if base <= 0.0 or height <= 0.0 or length <= 0.0:
    led("red")
    print("Invalid input")
else:
    area = 0.5 * base * height
    print("Area of the triangle is", round(area, 2))
    volume = area * length
    print("Volume is {:<8.2f} cubic units".format(volume))
    forward(60, distance=min(volume / 10, 60))

print("Goodbye")
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 Edexcel GCSE Computer Science 2023 Paper 2 Question 4?

Input base, height and length as floats. If any is less than or equal to 0, print an error. Otherwise print the area, 0.5 * base * height, rounded to 2 places, and print the volume, area * length, with "{:<8.2f} cubic units".format(volume). Print a goodbye message in every case.

How do I format a number to two decimal places in Python?

Use a format string: "{:.2f}".format(value). Add a width before the point, such as {:8.2f}, to make it take up 8 columns.

What is the difference between round() and format()?

round() gives back a number, which may print with fewer decimal places (2.5, not 2.50). format() gives back a string laid out exactly as you ask, so 2.5 becomes "2.50".

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F1.9 Arithmetic operators Programming basics
  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 Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.