Skip to content

Commit a3c5167

Browse files
pep style and fixed exception on input other and integer type
1 parent 7c90322 commit a3c5167

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

dynamic_programming/fibonacci.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
"""
22
This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem.
33
"""
4-
class Fibonacci:
54

5+
6+
class Fibonacci:
67
def __init__(self, N=None):
78
if N:
9+
N = int(N)
810
self.fib_array = [0] * (N + 1)
911
self.fib_array[0] = 0
1012
self.fib_array[1] = 1
1113
for i in range(2, N + 1):
1214
self.fib_array[i] = self.fib_array[
13-
i - 1] + self.fib_array[i - 2]
15+
i - 1] + self.fib_array[i - 2]
1416
else:
1517
self.fib_array = [None] * (N + 1)
1618

@@ -43,12 +45,13 @@ def get(self, sequence_no=None):
4345
"\n********* Enter different values to get the corresponding fibonacci sequence, enter any negative number to exit. ************\n")
4446
while True:
4547
print("Enter value: ", end=" ")
46-
i = eval(input())
47-
if i < 0:
48-
print("\n********* Good Bye!! ************\n")
49-
break
50-
fib.get(i)
51-
except NameError:
52-
print("\nInvalid input, please try again.")
48+
try:
49+
i = eval(input())
50+
if i < 0:
51+
print("\n********* Good Bye!! ************\n")
52+
break
53+
fib.get(i)
54+
except NameError:
55+
print("\nInvalid input, please try again.")
5356
except NameError:
5457
print("\n********* Invalid input, good bye!! ************\n")

0 commit comments

Comments
 (0)