83.7k views
5 votes
Can someone please tell, me whats wrong with this code....its supposed to give the fibonacci sequence as output but its giving 0 as output instead​

Can someone please tell, me whats wrong with this code....its supposed to give the-example-1
User Nicowernli
by
6.7k points

1 Answer

5 votes

def series(n):

n1 = 0

n2 = 1

count = 0

if n <=0:

print("Please enter a positive integer")

if count ==0:

print(0)

print(1)

count +=2

while count < n:

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

print(nth)

series(7)

I've written the working code. You added a break statement which right under a print function, which only printed the value of n1 and by looking at your variables, n1 = 0. That's why it was only printing 0.

User UID
by
6.4k points