Edexcel GCSE Computer Science June 2024 Paper 2, Question 5: the pasta menu

Pearson Edexcel 1CP2/02 June 2024, Question 5: complete getChoice, getShape and addShape subprograms and a main menu loop for a list of pasta shapes, using parameters, return values, random.randint and append. A model answer and the fifteen marks.

Past paper questionPearson 1CP2/02June 2024 Paper 215 marksComplete the program

Question 5 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 21 May 2024 (1CP2/02) is a 15 mark question built round three subprograms and a menu loop. The shape is one you will meet again and again: a loop that shows a menu, reads a choice, and calls a subprogram for it.

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

Pasta shape names are in a one-dimensional list. The user sees a menu: get a random shape, add a shape, show all the shapes, or exit. The program loops until exit is chosen.

You complete getChoice(), which reads a menu number and returns it; getShape(pTable), which returns a random shape from the list; addShape(pTable), which reads a new name and adds it to the end; and the main program, which calls the right subprogram for each choice, displays the shape from getShape(), and tells the user when a number is not on the menu. The file gives you constants for the menu numbers.

A model answer

import random

GET_SHAPE = 1
ADD_SHAPE = 2
SHOW_SHAPES = 3
EXIT = 4

pastaShapes = ["Bigoli", "Strozzapreti", "Trofie", "Gigli", "Chitarra", "Penne"]

def getChoice():
    print("1 Get a shape  2 Add a shape  3 Show shapes  4 Exit")
    choice = int(input("Choice: "))
    return choice

def getShape(pTable):
    index = random.randint(0, len(pTable) - 1)
    return pTable[index]

def addShape(pTable):
    newShape = input("New shape: ")
    pTable.append(newShape)

def showShapes(pTable):
    for shape in pTable:
        print(shape)
    print(len(pTable), "items")

choice = getChoice()
while choice != EXIT:
    if choice == GET_SHAPE:
        print(getShape(pastaShapes))
    elif choice == ADD_SHAPE:
        addShape(pastaShapes)
    elif choice == SHOW_SHAPES:
        showShapes(pastaShapes)
    else:
        print("Invalid choice")
    choice = getChoice()

Where the fifteen marks are

Nine single marks: getChoice() returns the choice; a random number is generated; its upper bound comes from the length of the list; one-dimensional indexing gets the shape; the new name is appended; the two list subprograms use their parameter pTable, not the global; a condition-controlled loop that runs until exit; selection that handles every option including an error; and a final call to getChoice() inside the loop.

Then up to 3 for design (the constants used for the menu, getShape() written as a function that returns) and up to 3 for how well it runs.

Where the marks are lost

  • pastaShapes inside getShape() or addShape(). They are given a parameter, pTable. Using the global instead loses a mark.
  • random.randint(0, 5). After a shape is added there are seven. The bound must be len(pTable) - 1.
  • print(choice) in getChoice(). It must return the choice, or the main program never sees it.
  • No second getChoice() at the end of the loop. The same choice repeats for ever.
  • Numbers instead of the constants in the ifs. The constants are given for a reason.

Run it

A working menu. Add a shape, then show the shapes, then get random ones until the new one comes up.

Choose 3 to see six shapes, 2 to add one, 3 again to see seven, then 1 for random shapes and 4 to exit. 5 is an invalid choice.
The program
from bugbot import *
import random
connect()

GET_SHAPE = 1
ADD_SHAPE = 2
SHOW_SHAPES = 3
EXIT = 4

pastaShapes = ["Bigoli", "Strozzapreti", "Trofie", "Gigli", "Chitarra", "Penne"]

def getChoice():
    print("1 Get a shape  2 Add a shape  3 Show shapes  4 Exit")
    choice = int(input("Choice: "))
    return choice

def getShape(pTable):
    index = random.randint(0, len(pTable) - 1)
    return pTable[index]

def addShape(pTable):
    newShape = input("New shape: ")
    pTable.append(newShape)

def showShapes(pTable):
    for shape in pTable:
        print(shape)
    print(len(pTable), "items")

choice = getChoice()
while choice != EXIT:
    if choice == GET_SHAPE:
        print(getShape(pastaShapes))
    elif choice == ADD_SHAPE:
        addShape(pastaShapes)
    elif choice == SHOW_SHAPES:
        showShapes(pastaShapes)
    else:
        print("Invalid choice")
    choice = getChoice()
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

How do I write a menu loop in Python?

Read the choice once before a while loop that runs until the exit value. Inside, use if and elif to call the subprogram for each option, with an else for an invalid choice, then read the choice again as the last line of the loop.

What is the difference between a parameter and a global variable inside a subprogram?

A parameter is the value handed to the subprogram when it is called, so the subprogram works on whatever it is given. Reading a global directly ties the subprogram to one particular variable. Exam questions that give a parameter expect it to be used.

How do I add an item to the end of a list in Python?

Use the append method: pTable.append(newShape).

More from this paper

Every Edexcel 1CP2 question we have worked

Learn it step by step

  1. F4.2 Parameters and return values Functions and structured code
  2. F3.3 Lists: one-dimensional arrays Strings, lists and records
  3. 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 Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.