Reputation: 25
I'm trying to write a piece of code that will compute values of wavelengths in such a way that for while n = 1, it checks the values for k = 2, 3, 4, 5 and 6. And I need to to do that up to n = 4, with 5 sequential integers k starting from n + 1. IE if n = 3, check for k = 4, 5, 6, 7, and 8. So there will be a total of 20 wavelengths. The code I currently have prints out 20 wavelengths, with the first one being correct for each n, but the next 4 are incorrect. I'm not sure how to fix this, please help
for n in range (1, 5):
for k in range (2, 7):
k = n + 1
RH = 1.09678e-2
waveLength = 1 / (R_H * (1/n**2 - 1/k**2))
print(waveLength)
Upvotes: 0
Views: 32
Reputation: 780663
Your inner loop is using the same k = n + 1
value every time, not using the value of k
that comes from range(2, 7)
.
The range in the inner loop needs to be based on n
, not fixed numbers.
for n in range(1, 5):
for k in range(n+1, n+6):
RH = 1.09678e-2
waveLength = 1 / (R_H * (1/n**2 - 1/k**2))
print(waveLength)
Another way to do it would be to use range(1, 6)
in the inner loop, and add the two values:
for n in range(1, 5):
for m in range(1, 6):
k = n + m
RH = 1.09678e-2
waveLength = 1 / (R_H * (1/n**2 - 1/k**2))
print(waveLength)
Upvotes: 2