forked from puja0902/Assignment-6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
38 lines (33 loc) · 1.2 KB
/
Copy pathfibonacci.py
File metadata and controls
38 lines (33 loc) · 1.2 KB
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
"""
fibonacci.py:
Iterable which produces an iterator of the Fibonacci series
for a given value
"""
class Fibonacci:
"""An iterable to generate Fibonacci series."""
def __init__(self, stop):
""" Requires a single integer as input."""
if not isinstance(stop, int):
raise ValueError
self.stop = stop # number of iterations
self.current = 0 # current number
self.position = 0 # position in Fibonacci series
self.previous = 0 # previous value in series
def __iter__(self):
"""Returns the instance object which is an iterator."""
return self
def __next__(self):
"""Defines the instance object as an iterator."""
if self.position <= self.stop:
if self.position == 0:
self.current += 1
next_number = 0
elif self.position == 1:
next_number = 1
else:
self.previous, self.current = (self.current,
self.current + self.previous)
next_number = self.current
self.position += 1
return next_number
raise StopIteration