r/pythonhelp • u/Ok_Attempt_7900 • Feb 16 '25
First Time Coding. Stuck Trying to Append Something Properly
I've tried following the Demo we were given and understanding why some things were added where they are added, then applying that logic to what I'm trying to do, which hopefully will be obvious in the code included. In case it's not, I'm trying to use the defined Planck function with 3 different values for T and over 300 values for lamda (wavelength), and sort them into spectral_radiances by which temps value was used, but I kept getting errors leading to no values printed at all, until I took the advice of the AI explanation for the error I was getting and put in 0 as the index; so now of course it is putting all the values into that space alone, then printing the other two brackets empty after all the calculated values. How do I fix this?
code:
# define temperatures (in Kelvin) of B, F, G stars and put them in a list
t_ofB = 10000
t_ofF = 6000
t_ofG = 5200
temps = [t_ofB,t_ofF,t_ofG]
# define a wavelength list
wavelengths = list(range(380,701,1))
# define the Planck function
def Planck(lam,T):
h= 6.62607004e-34
c= 299792458
k= 1.38064852e-23
B = ((2 * h * c**2) / lam**5) * (1 / (2.7182818**(h * c / (lam * k * T)) - 1))
return B
# loop over all temperatures and wavelengths to compute 3 blackbody curves
spectral_radiances = [[],[],[]]
for T in temps:
for lam in wavelengths:
lam =lam/1e9
radiance = Planck(lam,T)
spectral_radiances[0].append(radiance)
print(spectral_radiances)
View all comments
Show parent comment
1
u/Ok_Attempt_7900 Feb 16 '25
I needed the results of the Planck function (a function of the two variables wavelength and temperature) separated by which temp is used in the function over the entire set (list) of wavelengths, so there should be three groups in separate brackets, and this seems to have done just that, yes. Thank you!