Skip to content

Instantly share code, notes, and snippets.

@liamlah
Created February 9, 2012 06:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save liamlah/1777978 to your computer and use it in GitHub Desktop.
Save liamlah/1777978 to your computer and use it in GitHub Desktop.
Python Frequency/wavelength converter
print "This script can convert wavelengths to frequencies or vice versa."
speedlight = raw_input("Would you like an (1)exact value of the speed of light, or the (2)rounded value?\n" )
if speedlight == "1":
sol = 299792458.00
elif speedlight == "2":
sol = 3e8
else:
print "Please choose a valid option."
#sol = 299792458.00 #this is the speed of light in m/s
#sol = 3e8 #this is the speed of light in m/s rounded
#attempting split up of formulae into functions
def bands(wavelength):
# targetband == ()
if wavelength >=1000:
return "Theoretically long Radio wave"
if wavelength <= 1000 and wavelength >= 0.01:
return "Radio Wave"
elif wavelength <= 0.01 and wavelength >= 0.00001:
return "Microwave"
elif wavelength <= 0.00001 and wavelength >= 0.0000005:
return "Infra Red"
elif wavelength <= 0.0000005 and wavelength >= 0.00000001:
return "Visible Light"
elif wavelength <= 0.00000001 and wavelength >= 0.0000000001:
return "Ultra Violet"
elif wavelength <= 0.0000000001 and wavelength >= 0.000000000001:
return "X-Rays"
elif wavelength <= 0.000000000001:
return "Gamma Rays"
else:
return "Not within a range generally observed"
def wavelength():
print "Ok, Please enter your wavelength in Metres. (syntax for scientific notation is e.g. '1000 = 1e3' or '0.001 = 1e-3')"
wavelength = float(raw_input ())
answerfreq = sol / wavelength
print "the frequency is %rhz" % (answerfreq)
print "%r" % bands(wavelength)
#hash out below this line to test the new function
# if wavelength >=1000:
# print "Theoretically long Radio wave"
# if wavelength <= 1000 and wavelength >= 0.01:
# print "Radio Wave"
# elif wavelength <= 0.01 and wavelength >= 0.00001:
# print "Microwave"
# elif wavelength <= 0.00001 and wavelength >= 0.0000005:
# print "Infra Red"
# elif wavelength <= 0.0000005 and wavelength >= 0.00000001:
# print "Visible Light"
# elif wavelength <= 0.00000001 and wavelength >= 0.0000000001:
# print "Ultra Violet"
# elif wavelength <= 0.0000000001 and wavelength >= 0.000000000001:
# print "X-Rays"
# elif wavelength <= 0.000000000001:
# print "Gamma Rays"
# else:
# print "Not within a range generally observed"
def frequency():
print "Ok, Please enter your frequency in Hz. (syntax for scientific notation is e.g. '1000 = 1e3' or '0.001 = 1e-3')"
frequency = float(raw_input ())
wavelength = sol / frequency
print "the wavelength is %rm " % wavelength
print "%r" % bands(wavelength)
#hashed out calculation, has now been moved to new function.
# if wavelength >=1000:
# print "Theoretically long Radio wave"
# if wavelength <= 1000 and wavelength >= 0.01:
# print "Radio Wave"
# elif wavelength <= 0.01 and wavelength >= 0.00001:
# print "Microwave"
# elif wavelength <= 0.00001 and wavelength >= 0.0000005:
# print "Infra Red"
# elif wavelength <= 0.0000005 and wavelength >= 0.00000001:
# print "Visible Light"
# elif wavelength <= 0.00000001 and wavelength >= 0.0000000001:
# print "Ultra Violet"
# elif wavelength <= 0.0000000001 and wavelength >= 0.000000000001:
# print "X-Rays"
# elif wavelength <= 0.000000000001:
# print "Gamma Rays"
# else:
# print "Not within a range generally observed"
##old band calculation, obselete
#def frequency():
# print "Ok, Please enter your frequency in Hz. (syntax for scientific notation is e.g. '1000 = 1e3')"
# frequency = float(raw_input ())
# wavelength = sol / frequency
# print "the wavelength is %rm " % wavelength
# if wavelength >= 1000:
# print "Radio Wave"
# elif wavelength in range (0.01, 0.00001):
# print "Microwave"
# elif wavelength in range (0.00001, 0.0000005):
# print "Infra Red"
# elif wavelength in range (0.0000005, 0.00000001):
# print "Visible Light"
# elif wavelength in range (0.00000001, 0.0000000001):
# print "Ultra Violet"
# elif wavelength in range (0.0000000001, 0.000000000001):
# print "X-Rays"
# elif wavelength <= 0.000000000001:
# print "Gamma Rays"
# else:
# print "not an existing band"
choice_options = {
1: wavelength,
2: frequency,
}
choice = 0
while choice not in choice_options:
print "do you have (1) a wavelength, or (2) a frequency?"
choice = int(raw_input())
choice_options[choice]()
#next need to do defs
#you should bind the band calcs to a def, if wavelength == lamdaband try i the def "if band == range print microwave # done
# you can ask what unit they want to use and you should be able to change the value for SOL to create the appropriate output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment