1

I want to generate a sequence of numbers starting from 2 and calculating each successive number in the sequence as the square of the previous number minus one.

x = 2
while 0 <= x <= 10:
    x ** 2 - 1

print(x)

note: Iam interested in finding the first number in the sequence greater than ten or less than zero

but the loop keeps repeating with the same answer of 3. how do i stop it?

3
  • 3
    x never changes, what do you expect? Commented Aug 10, 2016 at 16:27
  • Put the print inside the while and follow the answer below Commented Aug 10, 2016 at 16:28
  • Use python -m pdb <your-script> to debug your script and see why it won't work, when you stepwise run through your endless loop. Commented Aug 10, 2016 at 16:32

2 Answers 2

4

you're not affecting x

x = 2
while 0 <= x <= 10:
    x = x ** 2 - 1

print(x)
Sign up to request clarification or add additional context in comments.

Comments

2

You have to update the value of x so that the while statement will eventually be false.

x = 2
while 0 <= x <= 10:
    x = x ** 2 - 1
print(x)

Comments

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.