[VISIBLE ONLY LOCALLY] Optionally view what the website will look like at another time (in PDT):
Credit: Problem written by Chris Piech, discovered by Einstein
One of the exciting human discoveries is that mass and energy are interchangable and are related by the equation E = M × C 2. This relationship was uncovered by Albert Einstein almost 100 years ago and we now know that the sun produces its heat by transforming small amounts of mass into large amounts of energy.
Write a program that continually reads in mass from the user and then outputs the amount of energy. Show your work in the following way:
Enter kilos of mass: 27 E = m * C^2 ... m = 27.0 kg C = 299792458m/s 2.4266389825894077e+18 joules of energy! Enter kilos of mass: 15 E = m * C^2 ... m = 15.0 kg C = 299792458m/s 1.3481327681052265e+18 joules of energy! Enter kilos of mass: 2 E = m * C^2 ... m = 2.0 kg C = 299792458m/s 1.7975103574736352e+17 joules of energy! Enter kilos of mass: 0.00000001 E = m * C^2 ... m = 1e-08 kg C = 299792458m/s 898755178.7368177 joules of energy!
If you ask the user to input kilograms, and you use speed of light equals 299792458 (which is in meters per second) the result of calculating E = M × C 2 will be in Joules.
Note that 3.0E8 is scientific notation for 3.0 × 10 8... which is 300,000,000. You don't need to do anything to print doubles in scientific notation. It will automatically do so when numbers get big enough.
Since the speed of light will never change, it makes sense to declare it as a constant.
""" File: emc2.py ------------------- This program helps users calculate how much energy they could get if they transformed their mass. Thanks Einstein! """ # This declares a constant for the speed of light in meters per second. C = 299792458 def main(): # Loop forever while True: # Read the mass in from the user. mass_in_kg = float(input("Enter kilos of mass: ")) # Calculate energy energy_in_joules = mass_in_kg * C * C # Display work to the user print("E = m * C^2 ...") print("m = " + str(mass_in_kg) + " kg") print("C = " + str(C) + "m/s") print(str(energy_in_joules) + " joules of energy!") print() if __name__ == '__main__': main()