Skip to content

Course home

Sign up

Develop code

EasyMediumHard
123456789101112131415
Question 8

A program is being developed to manage a database of core sample densities (measured in g/cm3\text{g/cm}^3g/cm3) collected from a geological survey. The densities are stored in a one-dimensional array.

The program displays a menu of options to the user. When the user selects an option, the program performs that task. The program loops continuously until the user selects the exit option.

You are provided with the following incomplete Python code:

import random

# Global list of geological core sample densities
densities = [2.65, 3.12, 2.87, 3.45, 2.91]

def displayMenu():
    print("
--- CORE SAMPLE ANALYZER ---")
    print("1. Analyze Random Sample Pressure")
    print("2. Add New Sample Density")
    print("3. Exit")

def getChoice():
    # TODO: Complete this subprogram
    # Prompt the user to enter their menu choice (no validation required)
    # Return the choice as an integer to the main program
    pass

def getSamplePressure():
    # TODO: Complete this subprogram
    # Use a random index to select a density from the densities list
    # Calculate pressure using: pressure = density * 98.1
    # Return the calculated pressure to the main program
    pass

def addSample():
    # TODO: Complete this subprogram
    # Allow the user to enter a new core sample density (as a decimal/float)
    # Append this new value to the end of the densities list
    pass

# --- Main Program ---
# TODO: Complete the main program loop
# Display the menu and loop until the user chooses option 3 (Exit)
# Call the correct subprograms based on the user's choice
# Display the pressure returned from getSamplePressure(), rounded to 2 decimal places
# Display an appropriate error message if an invalid option is entered

Amend the code to fully implement these subprograms and the main program logic.

[15]
Markscheme

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank