-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci_series.py
More file actions
32 lines (27 loc) · 869 Bytes
/
fibonacci_series.py
File metadata and controls
32 lines (27 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# To print the Fibonacci series
def fib(n): # defines a function to find the fibonacci no. of given index using recursion
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
terms = int(input("Please, enter the no. of terms of Fibonacci series to show: "))
fib_list = []
i = 0
if terms <= 0: # in case the no. given as input is -ve or 0
print("Invalid Input\n")
else:
while i < terms:
fib_list.insert(i, fib(i)) # the fibonacci nos. are stored in a list
i += 1
print(*fib_list, sep=", ", end="\n\n") # prints the list elements separated by ", "
"""
OUTPUT:
# 1
Please, enter the no. of terms of Fibonacci series to show: 10
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
# 2
Please, enter the no. of terms of Fibonacci series to show: -1
Invalid Input
"""