Python - Einstein's Theory of Special Relativity
by matt392 in Circuits > Software
60 Views, 1 Favorites, 0 Comments
Python - Einstein's Theory of Special Relativity
print ("This program will calculate the Theory of Special Relativity.") # Speical Relativity Formula is: E = mc**2 # E is Energy; m is Mass; c is the speed of light # There are 2 sections of the program based on what the user wants to solve for # Function to solve for Energy # Solve for E: enter m (c is a constant; 3 * (10**8) meters per second) # Formula: E = mc**2 def solveForEnergy(): print ("Solving for Energy:") mass = int(input("Please enter the mass: ") ) # Speed of light is a constant c = 299792458 # Multiply mass times [speed of light squared] energy = mass * c**2 print("The force is:", energy, "joules") # Function to solve for mass # Solve for mass: enter Energy # Formula: Mass = E/(c**2) def solveForMass(): print ("Solving for mass:") energy = int(input("Please enter the Energy: ") ) # Speed of light is a constant c = 299792458 # Divide energy by [speed of light squared] mass = energy/(c**2) print("The mass is: ", mass) continueCalculations = "y" while (continueCalculations=="y"): # Ask user if they want to solve for force, mass or acceleration print ("If you would like to solve for energy, enter 1.") print ("If you would like to solve for mass, enter 2.") choiceToSolve = int(input("Enter 1 or 2: ") ) # if choiceToSolve is 1, run function solveForEnergy() # if choiceToSolve is 2, run function solveForMass() if choiceToSolve == 1: solveForEnergy() elif choiceToSolve == 2: solveForMass() else: print("Please enter 1 or 2.") continueCalculations = input("Would like to do another calculation of Einstein's Law of Special Relativity? (y/n): ") print("==================================") print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")