Skip to content

Commit fdc6be7

Browse files
committed
Updated the bug in Fibonacci
Signed-off-by: pavansai5448 <[email protected]>
1 parent 989a935 commit fdc6be7

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
#function which finds out individual term of the fibonacci sequence
2-
def fibo(x):
3-
if(x == 1 or x == 2): #base case
4-
return 1
1+
def fibo(n):
2+
if n <= 0:
3+
return []
4+
elif n == 1:
5+
return [0]
6+
elif n == 2:
7+
return [0, 1]
58
else:
6-
return fibo(x-1) + fibo(x-2)
7-
#following the actual way how the fibonacci series is generated i.e. adding the previous two terms
9+
sequence = [0, 1]
10+
for i in range(2, n):
11+
sequence.append(sequence[i-1] + sequence[i-2])
12+
return sequence
813

914
n = 0
10-
while(n<=0):
11-
n = int(input("Enter how many terms do you need in your Fibonacci Sequence? "))
12-
#this loop is just to ensure that the user inputs a valid number
15+
while n <= 0:
16+
n = int(input("Enter how many terms you need in your Fibonacci Sequence: "))
1317

14-
for i in range(1,n):
15-
#finding out individual terms of the series and printing till (n-1) terms since we don't want an extra comma at the end
16-
print(fibo(i), end=", ")
17-
print(fibo(n)) #printing the last(i.e. nth term)
18+
fib_sequence = fibo(n)
19+
print(", ".join(map(str, fib_sequence)))

0 commit comments

Comments
 (0)