r/pythonhelp • • Aug 16 '22

problem with Fibonacci sequence stuck on zeros??

def series(n):
  n1 = 0
  n2 = 1
  count = 0
if n <= 0:
print("enter a posotive integer")
if n >= 0:
while count <= n:
print(n1)
      nth=n1+n2
      n2 = n1
      nth = n2
      count+=1
series(5)

2 Upvotes

8 comments sorted by

1

u/Aryangupt556 Aug 16 '22

its does output 0,0,0,0,0 anyone know why??

1

u/slowlycatchiemonkey Aug 16 '22

You never update the value of n1, so it is always zero, and that is what you are printing each time.

1

u/Aryangupt556 Aug 16 '22

How would I fix it ?

2

u/slowlycatchiemonkey Aug 16 '22

You need to understand what your own code is doing. Print out the values of each variable for series(2) and see how they change.

1

u/Aryangupt556 Aug 16 '22

I see so it’s only printing n1 and n2 with the number I implemented

1

u/Aryangupt556 Aug 17 '22

Update : fixed dumb mistake what I had to do was n1=n2 and nth=n2 and the whole thing worked can’t believe how a little mistake can impact the whole code 🤦🤦

1

u/slowlycatchiemonkey Aug 16 '22

Well, you do update n2, but back to n1 (0).

Think about what the Fibonacci sequence is actually doing - break that down into small steps.

Part of coding is debugging - give yourself some print output to actually see what's going on

2

u/Aryangupt556 Aug 16 '22

Ok thanks I try again I’ve got a little more understanding thanks