Python - Energy of a Photon

by matt392 in Circuits > Software

45 Views, 1 Favorites, 0 Comments

Python - Energy of a Photon

EnergyOfPhoton.jpg
print ("This program will calculate the energy of a photon.")

import math

#  The energy of a photon forumula is:
#  Energy = [Plank's Constant][(Speed of Light)/(Wavelength of light)]
###########################################

# 2 functions: solve for energy or wavelength of light

#  Function to solve for energy
#    Solve for e: enter w (wavelength) 
#    Formula: energy = [(planksConstant) * (speedOfLight/wavelength)]
def solveForEnergy():
    print ("Solving for Energy of Photon")
    planksConstant = ( 6.62607004*(10**-34) )
    speedOfLight = 299792458
    wavelength = float(input("Enter the wavelength: ") )
    energy = ( (planksConstant)*(speedOfLight/wavelength) )
    print("The energy is:", energy)

#  Function to solve for wavelength
#  wavelength =  [(planksConstant)*(speedOfLight)]/energy
#    Solve for wavelength:  
def solveForWavelength():
    print ("Solving for Wavelength")
    planksConstant = ( 6.62607004*(10**-34) )
    speedOfLight = 299792458
    energy = float(input("Please enter the energy: ") )
    wavelength = (planksConstant*speedOfLight)/energy
    print("The wavelength is:", wavelength)

continueCalculations = "y"

# Check to see if the user wants to continue to calculate the energy of a photon
while (continueCalculations=="y"):

 #  Ask user if they want to solve for energy or wavelength
    print ("If you would like to solve for energy, enter 1.")
    print ("If you would like to solve for wavelength, enter 2.")
    choiceToSolve = int(input("Enter 1 or 2: ") )

# if choiceToSolve is 1, run function solveForEnergy()
# if choiceToSolve is 2, run function solveForWavelength()
    if choiceToSolve == 1:
        solveForEnergy()
    elif choiceToSolve == 2:
        solveForWavelength()
    else:
        print("Please enter 1 or 2")
    
    continueCalculations = input("Would like to do another calculation for the energy of a photon? (y/n): ")
    
print("==================================")
print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")

Downloads