Skip to content

Commit 6a55d0e

Browse files
committed
Add Fibonacci Seq
1 parent 0c79012 commit 6a55d0e

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

FibonacciSequence_Solution1.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Authour: azmathias Stuperuser Ltd
2+
# Purpose: This program uses a function that allows the user to find the value of the nth term in the sequence by inputting the desired term.
3+
# Notes: This solution uses a loop (less performant at as n increases).
4+
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181....]
5+
# ToDo: More testing
6+
7+
# get nth term to test
8+
def inputterm(message):
9+
while True:
10+
try:
11+
userinput = int(input(message))
12+
except ValueError:
13+
print("\nPlease input a valid number:")
14+
continue
15+
else:
16+
return userinput
17+
break
18+
19+
# return Fibonacci sequence to nth term
20+
def fibonacci(nth):
21+
n1,n2 = 0,1
22+
for i in range(0,nth-1):
23+
n1,n2 = n2, n1+n2
24+
return(n1)
25+
26+
27+
# allow user to test again
28+
def testStatus():
29+
testQ = "\nType q to quit, or hit <Enter> to test another number."
30+
print(testQ)
31+
new_value = 0
32+
while not new_value:
33+
response = input('> ')
34+
if response == '':
35+
print()
36+
elif response == 'q':
37+
new_value = -1
38+
else:
39+
print("\nPlease try again.")
40+
print(testQ)
41+
continue
42+
return new_value
43+
44+
def main():
45+
print("Fibonacci Sequence Term Finder \n")
46+
value = 0
47+
while value != -1:
48+
num = inputterm("Please enter a number for the nth term:")
49+
print(fibonacci(num))
50+
value = testStatus()
51+
print("\nThank you for playing!")
52+
53+
54+
55+
if __name__ == '__main__': main()
56+

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Python Mini Project
1+
## Python Projects
22

33
Although I can code in other languages, I am new to Python. This repo contains projects developed as I extend my Python coding skills.
44
Most are sourced from those listed in https://github.com/jorgegonzalez/beginner-projects.
@@ -63,8 +63,9 @@ There are two solutions; the first using a loop (less performant at as n increas
6363

6464
**What I learned:**
6565
1. If..else statements
66-
2. While loops
66+
2. Parallel assignment
6767
3. Recursive functions
68+
4. Performance benchmarks for functions
6869

6970

7071

0 commit comments

Comments
 (0)