-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiterator_part2.py
More file actions
34 lines (26 loc) · 871 Bytes
/
iterator_part2.py
File metadata and controls
34 lines (26 loc) · 871 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
33
34
#here we attempt to create a iterator that will return the fibonacci series
class fibonacci:
def __init__(self,n):
self.n=n
self.v=1 #Track of iteration index
self.first=0 #first variable
self.second=1 #second variable
self.third=0 #third variable
def __iter__(self):
return self
def __next__(self):
if self.v>self.n:
raise StopIteration
else:
#Temporarily storing value since we have to return it
x=self.first
#incrementing counter variable
self.v+=1
#swapping values between variables
self.third=self.first+self.second
self.first=self.second
self.second=self.third
return(x)
series_length=10
for i in fibonacci(series_length):
print(i)