Python - Calculate Escape Velocity From Star/Planet/Black Hole

by matt392 in Circuits > Software

48 Views, 1 Favorites, 0 Comments

Python - Calculate Escape Velocity From Star/Planet/Black Hole

escapevelocities.png
print ("This program will calculate the Escape Velocity.")

#  Import the math module so the sqrt() function can be used
import math

#  Escape Velocity Forumula is: velocity = Square Root [(2 * G * M)/R]
#  G is a constant: 6.67 x 10**-11
#  M = mass of the object you are escaping from
#  R = radius of the object

#  Function to solve for velocity
#    Solve for v: enter m (Mass) and r (radius of object)
#    (G is a constant; 6.67 x 10**-11)
#    Formula: v = square root [(2 * G * M)/R]
#   User can enter mass and radius in exponential notation
def solveForEscapeVelocity():
    print ("Solving for Escape Velocity")
    print ("Please enter the mass in kilograms in exponential notation: ") 
    massBase = int(input("Please enter the base: ") )
    massExponent = int(input("Please enter the exponent: ") )
    massWholeNumber = int(input("Please enter the whole number: ") )
    massBaseToPower = math.pow(massBase,massExponent)
    mass = massWholeNumber * massBaseToPower
    
    print ("Please enter the radius in meters in exponential notation: ")
    radiusBase = int(input("Please enter the base: ") )
    radiusExponent = int(input("Please enter the exponent: ") )
    radiusWholeNumber = int(input("Please enter the whole number: ") )
    radiusBaseToPower = math.pow(radiusBase,radiusExponent)
    radius = radiusWholeNumber * radiusBaseToPower
    
    # G is a constant
    g =  (6.67 * (10**-11) )
    # Multiply (mass times G times 2)/radius
    velocityBeforeSquareRoot =  (mass * g * 2)/radius
    # Then take the square root
    velocity = math.sqrt(velocityBeforeSquareRoot)
    print("The velocity is:", velocity, "meters per second")
    
continueCalculations = "y"

# Check to see if the user wants to continue to calculate Escape Velocity
while (continueCalculations=="y"):
    solveForEscapeVelocity()
    continueCalculations = input("Would like to do another calculation for the Escape Velocity? (y/n): ")
    
print("==================================")
print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")


Downloads