Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im trying to take in a range on frequencys from a to b from the user and convert that range to wavelength and energy. I want to increment it in 10^2. Ive got the input but I cant get my for loop to work. It keeps saying my energy is 0

What I have tried:

Python
from scipy.constants import Planck

c=299792458

for i in range(a,b,10**5):
    energy=Planck*i
    wavelength=c/i
    
      
    print('{0:>18}'.format(i),'|'\
          '{0:>18.7f}'.format(wavelength), '|', \
          '{0:>18.7f}'.format(energy))


I've also tried this

Python
from scipy.constants import Planck

def energy(x,dx):
    return(Planck*x)
    
def wavelength(x,dx):
    return(c/x)

c=299792458
dx=10**5

for i in range(a,b,dx):
    x=i
    c_wave=wavelength(x,dx)
    c_energy=energy(x,dx)
    
    print('{0:>18}'.format(i),'|'\
          '{0:>18.7f}'.format(c_wave), '|', \
          '{0:>18.7f}'.format(c_energy))

I havent included the getting a and b parts in this but there is a section for the user to input a and b
Posted
Updated 15-Mar-20 22:05pm
v3

I think you have your range values round the wrong way. The correct way is
Python
range(start, end, step)

In your example your step value is 100,000, so it is likely that it will only do a single calculation.
 
Share this answer
 
Comments
CiaraMc96 15-Mar-20 14:34pm    
The steps meant to be 10^2, I just read it wrong. I got it working to print all the lines for energy but they all still say 0. The range seems to be workinf because it prints all the values between a and b with the step in between and calculates the correct wavelength for each value
Richard MacCutchan 15-Mar-20 18:24pm    
Then you need to check each calculation to see what values are being returned. Add some print statements so you can see all the values at each step.
The 'problem' is the Plank's constant.
It (and the energy of the single photon) is very, very small (about 6 * 10-34).
With the format you specified the code shows only zeroes.
This works for me
Python
import scipy.constants as const

for f in range(1,10**5, 10**2):
    energy=const.Planck*f
    wavelength=const.c/f
    print('{0:>18}'.format(f),'|'\
          '{0:>18.7f}'.format(wavelength), '|', \
          '{:>.2E}'.format(energy))
Output (excerpt of the)
('                 1', '| 299792458.0000000', '|', '6.63E-34')
('               101', '|   2968242.1584158', '|', '6.69E-32')
('               201', '|   1491504.7661692', '|', '1.33E-31')
('               301', '|    995988.2325581', '|', '1.99E-31')
('               401', '|    747612.1147132', '|', '2.66E-31')
('               501', '|    598388.1397206', '|', '3.32E-31')
('               601', '|    498822.7254576', '|', '3.98E-31')
('               701', '|    427663.9914408', '|', '4.64E-31')
('               801', '|    374272.7315855', '|', '5.31E-31')
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900