Yusuf Ning
Yusuf Ning

Reputation: 65

how to stop this while loop in python

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?

Upvotes: 1

Views: 113

Answers (2)

jackiezhu
jackiezhu

Reputation: 422

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)

Upvotes: 2

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140148

you're not affecting x

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

print(x)

Upvotes: 4

Related Questions