Jan 19, 2026 | 349 words | 3 min read
12.2.3. Task 3#
Learning Objectives#
Read and interpret a flowchart that contains user-defined functions; Design a program with user-defined functions; Investigate the advantages of user-defined functions in Python;
Introduction#
You find yourself stranded on an unknown planet. As an aspiring engineer, you decide to design a rocket capable of escaping the planet’s gravitational pull so you can return home. To do this, you must write a program that calculates the minimum velocity, known as escape velocity, required for your rocket to break free from the planet’s gravitational field.
The escape velocity of an object is given by
where
\(v_e\) is the escape velocity
\(G\) is the gravitational constant (\(\qty{6.6743 \times 10^{-11}}{\meter^{3}\per\kilo\gram\cdot\second\squared}\))
\(m\) is the mass of the planet, and
\(r\) is the radius of the planet
The mass of the planet is given by
where
\(\rho\) is the planet’s average density and
\(V\) is the volume.
Assuming the planet is spherical, its volume is given by
where
r is the radius.
Task Instructions#
Following the flowchart in Fig. 12.1, write a
Python program with three user-defined functions and a main
function that calculates the minimum velocity required for your rocket to break free
from the planet’s gravitational field. Your main function should initialize
the gravitational constant and then prompt the user for the planet’s average density and
radius, before calling each of the user-defined functions in turn and then displaying
the results.
In the calc_escape_velocity function, check that the radius and mass are
positive. If they are negative, print an error message as shown in the test cases, and
return \(0\) as the escape velocity. Otherwise, calculate and return the escape
velocity.
Note
The radius is converted from \(\meter\) to \(\kilo\meter\) before printing to the terminal.
Fig. 12.1 Escape velocity program flowchart.#
Sample Output#
Use the values in Table 12.6 below to test your program.
Case |
\(\rho\) (\(\frac{\kilo\gram}{\meter^3}\)) |
Radius (\(\meter\)) |
|---|---|---|
1 |
5513.0 |
6371000 |
2 |
1879.8 |
2574730 |
3 |
300.0 |
-600.0 |
4 |
-4200 |
6310100 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 py2_team_3_main_teamnumber.py Enter the average density of the planet: 5513.0 Enter the radius of the planet: 6371000 For a planet with radius 6,371.0 km and density 5,513.00 kg/m^3, the estimated escape velocity is 11,185.72 m/s
Case 2 Sample Output
$ python3 py2_team_3_main_teamnumber.py Enter the average density of the planet: 1879.8 Enter the radius of the planet: 2574730 For a planet with radius 2,574.7 km and density 1,879.80 kg/m^3, the estimated escape velocity is 2,639.67 m/s
Case 3 Sample Output
$ python3 py2_team_3_main_teamnumber.py Enter the average density of the planet: 300.0 Enter the radius of the planet: -600.0 Radius must be greater than zero! For a planet with radius -0.6 km and density 300.00 kg/m^3, the estimated escape velocity is 0.00 m/s
Case 4 Sample Output
$ python3 py2_team_3_main_teamnumber.py Enter the average density of the planet: -4200 Enter the radius of the planet: 6310100 Mass must be greater than zero! For a planet with radius 6,310.1 km and density -4,200.00 kg/m^3, the estimated escape velocity is 0.00 m/s
Deliverables |
Description |
|---|---|
py2_team_3_mass_teamnumber.py |
Your completed mass calculation function. |
py2_team_3_volume_teamnumber.py |
Your completed volume calculation function. |
py2_team_3_escape_velocity_teamnumber.py |
Your completed escape velocity function. |
py2_team_3_main_teamnumber.py |
Your completed main Python code. |