11import math
22
33
4- # Escape velocity calculator script
5- # Minor change to trigger pull request again
64def escape_velocity (mass : float , radius : float ) -> float :
75 """
8- Calculates the escape velocity needed to break free from a celestial body's gravitational field.
6+ Calculates the escape velocity needed to break free from a celestial body's
7+ gravitational field.
98
109 The formula used is:
1110 v = sqrt(2 * G * M / R)
11+
1212 where:
1313 v = escape velocity (m/s)
14- G = gravitational constant (6.67430 × 10^-11 m^3 kg^-1 s^-2)
14+ G = gravitational constant (6.67430 * 10^-11 m^3 kg^-1 s^-2)
1515 M = mass of the celestial body (kg)
1616 R = radius from the center of mass (m)
1717
18+ Source:
19+ https://en.wikipedia.org/wiki/Escape_velocity
20+
1821 Args:
1922 mass (float): Mass of the celestial body in kilograms.
2023 radius (float): Radius from the center of mass in meters.
@@ -36,14 +39,14 @@ def escape_velocity(mass: float, radius: float) -> float:
3639 ...
3740 ZeroDivisionError: Radius cannot be zero.
3841 """
39- G = 6.67430e-11 # Gravitational constant in m^3 kg^-1 s^-2
42+ gravitational_constant = 6.67430e-11 # m^3 kg^-1 s^-2
4043
4144 if radius == 0 :
4245 raise ZeroDivisionError ("Radius cannot be zero." )
4346 if mass == 0 :
4447 return 0.0
4548
46- velocity = math .sqrt (2 * G * mass / radius )
49+ velocity = math .sqrt (2 * gravitational_constant * mass / radius )
4750 return round (velocity , 3 )
4851
4952
@@ -54,11 +57,9 @@ def escape_velocity(mass: float, radius: float) -> float:
5457 print ("Calculate escape velocity of a celestial body...\n " )
5558
5659 try :
57- # User input
5860 mass = float (input ("Enter mass of the celestial body (in kg): " ).strip ())
5961 radius = float (input ("Enter radius from center (in meters): " ).strip ())
6062
61- # Result
6263 velocity = escape_velocity (mass , radius )
6364 print (f"Escape velocity is { velocity } m/s" )
6465
0 commit comments