Reputation: 13
I've been trying to generate a Fibonacci sequence using Python 3. Since I'm totally new to programming I'm using basic tools. Lists in this case, as an exercise. The problem, I have, is to stop the sequence when I want it to stop.
For example,I need a Fibonacci up to 100 so I wrote this:
fib = [1,2]
n = 0
while max(fib) <= 100:
fib.append(fib[n]+fib[n+1])
n = n+1
print(fib)
print(max(fib))
print(n)
The print()
statements are for my benefit only, so I would know what is going on.
In return I get:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
144
9
So everything is working fine except I intended the sequenced to be finished before it passes 100. Why I have the 144 there then? What am I doing wrong?
Upvotes: 1
Views: 208
Reputation: 1
I just wrote this code, If u have any question, I will try explain you every moment
def fibonacci(n):
if n < 0:
return 0
elif n == 0:
return 0
elif n == 1:
return 1
else:
a, b = 0, 1
for _ in range(1, n):
a, b = b, a + b
return b
n = int(input("Enter the Fibonacci number: "))
result = fibonacci(n)
print(f"The Fibonacci number number {n} is {result}")
Upvotes: 0
Reputation: 13
So many replies :)
In the end i went with the:
while fib[n]+fib[n+1] < 100
fib.append(fib[n]+fib[n+1])
n = n+1
which seemed to me to be the simplest (still much to learn)
But I'm glad that there were so many differnt approches shown here. I'll try to remeber them all. Moreover I learned where my logic was wrong and that's good too.
Thanks
Upvotes: 0
Reputation: 3520
The error you are getting is very simple. You are checking that the biggest number in fib
is less than or equal to 100, but when you add the last two items together the end value may be larger.
You can fix this by checking the new value before you add it to the list.
Also, in Python you can access the last item the list by doing
fib[-1]
Therefore you can remove n
by changing your code to use negative indexes.
Finally, the fibonnacci sequence should start with 1, 1 not 1, 2.
fib = [1, 1]
while True:
n = fib[-1] + fib[-2]
if n >= 100:
break
fib.append(n)
print(fib)
Upvotes: 1
Reputation: 41
When you do
while max(fib) <= 100:
you want to be checking whether the next number is greater than 100, but you are actually checking if the current largest number in the list is greater than 100.
So, instead of the program saying "144 is larger than 100, lets not add it", it's going "89 is less than 100, lets add 144!"
To fix this, you need to check how large the number you're adding next is. You can do this with an if
statement in your while loop, and having a different variable to hold whether you should quit the loop or not. For example:
fib = [1,1]
n = 0
quit = False
while not quit:
next_num = fib[n]+fib[n+1]
if next_num <= 100:
fib.append(next_num)
n = n+1
else:
quit = True
print(fib)
print(max(fib))
print(n)
Also, as Joe Iddon said, the Fibonacci sequence starts with "1, 1, 2"
Upvotes: 0
Reputation: 1337
just put an if statement inside your loop.
while max(fib) <= 100:
if(fib[n]+fib[n+1] > 100):
break
fib.append(fib[n]+fib[n+1])
n = n+1
What break statements do is, if the breaking condition is met, the program immediately gets out of the loop .
Also your looping condition is a bit inefficient since it has to check for the max value in the list fib on every iteration.
A simpler approach would be like this
while (fib[n]+fib[n+1] < 100):
fib.append(fib[n]+fib[n+1])
n = n+1
This way your code doesn't have to check for the max value on every iteration
Upvotes: 2
Reputation: 20414
A couple of things:
1, 1, 2, ...
100
, rather than checking if the next element is less than 100
.So, the first is obviously easy to correct, just declare fib
as [1, 1]
.
As for the second, you could either use a while True
loop which you then break
out of if the next element is greater than 100
, or you could have a variable which stores the next and just check that this is less than 100
as the evaluation for the while
.
So, after this line:
fib = [1, 1]
You could either use:
next = 2
while next <= 100:
fib.append(next)
next += fib[-2]
or
while True:
next = fib[-2] + fib[-1]
if next > 100:
break
fib.append(next)
which both give:
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Upvotes: 2
Reputation: 31
This is a very common mistake to make when you start programming :)
You are testing whether the maximum value in 'fib' is still under 100, and then you are adding a new number. So while you test, fib is still [1, 2, 3, 5, 8, 13, 21, 34, 55, 89] and then 144 gets added inside the loop.
Upvotes: 0
Reputation: 51335
As noted above, the issue is that your loop will still be executed one more time when max(fib)
is 89, as it is still less than 100. So, it will generate the 144 before finding a max(fib)
greater than 100.
While there are certainly other ways to do this, in keeping your general loop structure, you could do this:
while fib[n]+fib[n+1] < 100:
fib.append(fib[n]+fib[n+1])
n = n+1
>>> fib
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Upvotes: 0
Reputation: 969
There is nothing wrong, what you wanted is wrong.
It generates more than 100 because 89+55 is 144, you wanted the sequence to be less than 100, not their value after adding.
Try to use "if" statement in your code.
Upvotes: 0