0

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)
1
  • What should the result look like? Commented Sep 11, 2020 at 19:24

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

The result isn't different from OP's
Yes it is. He's ignoring the inner range, because he sets k = n + 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.