forked from PLPAfrica/Feb_2024-Python-Hackathon1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
32 lines (27 loc) · 944 Bytes
/
functions.py
File metadata and controls
32 lines (27 loc) · 944 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
def fibonacci(n):
"""
This function generates the Fibonacci sequence up to a specified term n using iteration.
Args:
n: The number of terms in the Fibonacci sequence.
Returns:
A list containing the Fibonacci sequence up to n terms.
"""
fibonacci_sequence = []
if n <= 0:
return fibonacci_sequence
elif n == 1:
fibonacci_sequence.append(0)
else:
fibonacci_sequence.extend([0, 1]) # If n is greater than 1, add the first two terms (0 and 1) to the sequence
a, b = 0, 1
for _ in range(2, n):
c = a + b
fibonacci_sequence.append(c)
a, b = b, c
return fibonacci_sequence
# Get the number of terms from the user
num_terms = int(input("Enter the number of terms: "))
# Generate the Fibonacci sequence
fibonacci_sequence = fibonacci(num_terms)
# Print the Fibonacci sequence
print(fibonacci_sequence)