Skip to content

Course home

Sign up

Subprograms

EasyMediumHard
1
Question 1

An astrophysics lab is developing software to calculate the escape velocity of various exoplanets.

The formula to calculate the escape velocity of a spherical celestial body is:

v=2GMR v = \sqrt{\frac{2GM}{R}} v=R2GM​​
  • vvv is the escape velocity in meters per second (m/s\text{m/s}m/s)
  • GGG is the universal gravitational constant, G=6.674×10−11 m3 kg−1 s−2G = 6.674 \times 10^{-11} \text{ m}^3 \text{ kg}^{-1} \text{ s}^{-2}G=6.674×10−11 m3 kg−1 s−2
  • MMM is the mass of the planet in kilograms (kg\text{kg}kg)
  • RRR is the radius of the planet in meters (m\text{m}m)

A program and subprogram have been started to carry out this calculation, but are incomplete.

Original code in file Q05.py:

# Q05.py - Unfinished program
# Do not change the existing input statements

def calculate_escape_velocity():
    # Add parameters and calculation here
    pass

# Main program
mass = float(input("Enter the mass of the planet (kg): "))
radius = float(input("Enter the radius of the planet (m): "))

# Call the subprogram and print the formatted result

Amend the program and subprogram to meet the following requirements:

  • The subprogram must work for any values of mass and radius passed as parameters. You can assume values passed to the subprogram will always be numbers. No validation is required.
  • The subprogram must calculate and return the escape velocity based on the input parameters using G=6.674×10−11G = 6.674 \times 10^{-11}G=6.674×10−11.
  • The main program must print the calculated velocity, formatted to show exactly three decimal places (e.g. 11186.273 or 2375.400).

Do not add any additional functionality.

Save your amended code as Q05FINISHED.py

[12]
Markscheme

Subprograms Questions

  1. GCSE
  2. /Computer Science
  3. /Subprograms

29 exam-style questions on Edexcel GCSE Computer Science Subprograms, covering Built-in and user-devised subprograms, Functions versus procedures, and Global and local variables. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank