1

I am trying to print fibonacci series using lists in python. This is my code

f=[1,1]
for i in range(8):
    f.append(f[i-1]+f[i-2])

print(f)

output is

[1, 1, 2, 3, 2, 3, 5, 5, 5, 8]

I am not getting the bug here!

3
  • Consider adding some debug output, such as what i, f, f[i-1], and f[i-2] are on each step. Commented May 23, 2017 at 17:14
  • consider what happens when i=2 - you've already appended to the list twice, but the next value you append is f[0] + f[1] Commented May 23, 2017 at 17:17
  • You are starting with the wrong list index for appending. You start with index 2 in the list, so you should also start your loop with that, i.e. range(2, 8) Commented May 23, 2017 at 17:17

6 Answers 6

3

Python's range starts at 0, you need to start with generating element 2. By starting at 0, you get negative indices for the first couple of calculations, which access from the end of the list. To fix this, change the loop to for i in range(2, 8).

To clarify what seems to be a source of confusion, by starting the range at zero and using negative indexing you end up with the following terms being summed and appended to the list:

f[2] = f[0-1] + f[0-2] = f[-1] + f[-2] (= f[1] + f[0]) = 1 + 1 = 2  # looking good
f[3] = f[1-1] + f[1-2] = f[0] + f[-1] (= f[0] + f[2]) = 1 + 2 = 3   # looking good
f[4] = f[2-1] + f[2-2] = f[1] + f[0] = 1 + 1 = 2                    # oops!
f[5] = f[3-1] + f[3-2] = f[2] + f[1] = 2 + 1 = 3

From that point on the numbers are on track, but offset from the proper indexing by 2.

Sign up to request clarification or add additional context in comments.

10 Comments

Maybe add that the reason the user doesn't get an indexing error is because indices of -1 and -2 will give the 1st and 2nd values starting from the end of the list.
@Green, and that's why he actually should be fine with f.append(f[-1] + f[-2]) regardless of the range limits.
@pjs f[-1] + f[-2] sums the two last elements.
@bereal I understand that. Now consider what happens after a few iterations when you have more than 2 elements, and you begin accessing positive indices using the range statement. It repeats summing the first couple of elements. That's why it incorrectly repeats "2, 3, 2, 3".
@pjs I perfectly understood what you meant before the first comment. f.append(f[-1] + f[-2]) is still correct. It doesn't even refer to i.
|
2
f=[1,1]

for i in range(8):
    f.append(f[i]+f[i+1])

print(f)

RESULT

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

1 Comment

@Chaitanya Patil your >> f.append(f[i-1]+f[i-2])<< I have changed the f[i-2] -> f[i+1]
1
f=[1,1]
for i in range(8):
    s=len(f)
    f.append(f[s-1]+f[s-2]) #sum of last two elements

print(f)

or use -1 and -2 as index for two last element.

Comments

0

Adding to pjs' answer: When i = 0, you are adding f[-1] and f[-2], where an index of -1 gives you the last element in the list and so on.

To fix that, as other answers have already pointed out, you need to set the correct starting point for range().

Comments

0

You should be aware that the range function starts at 0 if you don't explicitly specify a starting point. In your case, it should be: for i in range(2, 8).

Output:

[1, 1, 2, 3, 5, 8, 13, 21]

Also, in Python, you can use negative index in an array to count from the right instead of the left, this is why you had this weird result.

1 Comment

Thanq :-) got it
0

Script

f=[1,1]

for i in range(2,8):
  print(i)
  f.append(f[i-1]+f[i-2])
  print(f)

Output

2
[1, 1, 2]
3
[1, 1, 2, 3]
4
[1, 1, 2, 3, 5]
5
[1, 1, 2, 3, 5, 8]
6
[1, 1, 2, 3, 5, 8, 13]
7
[1, 1, 2, 3, 5, 8, 13, 21]

Issue is i starts from 0, f[-1], f[-2] returns 0. start using range from 2 and 8 represents first 8 fibonacci numbers.

removing debug print

Script

f=[1,1]

for i in range(2,8):
   f.append(f[i-1]+f[i-2])
print(f)

1 Comment

Thanks ! got it :-)

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.