forked from karan/Projects
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfibonacci.py
executable file
·44 lines (34 loc) · 946 Bytes
/
fibonacci.py
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
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
def fib(N):
"""
Recursively return the next term and the current term in the Fibonacci
sequence.
"""
if N == 1:
return [1, 0]
else:
terms = fib(N - 1)
terms = [terms[0] + terms[1], terms[0]]
return terms
def validate_positive_integer():
"""
Ask the user for input and only return when a positive integer under
500 is given.
"""
while True:
s = raw_input("Which term in the Fibonacci sequence you want to see? ")
try:
N = int(s)
if N >= 500:
print "Enter a number smaller than 500."
elif N > 0:
return N
else:
print "Enter a positive integer."
except ValueError:
print "Enter a positive integer."
def main():
N = validate_positive_integer()
print fib(N)[1]
if __name__ == "__main__":
main()