Solution Review: Create a Fibonacci Iterator
The solution to the 'Create a Fibonacci Iterator' challenge.
We'll cover the following
class Fibonacci:def __init__(self, n):self.limit = nself.count = 1self.a = 0self.b = 1def __iter__(self):return selfdef __next__(self):if self.count <= self.limit: # Controlled iterationsx = self.aself.a, self.b = self.b, self.a +self.b # Adding preceding valuesself.count += 1return xelse:raise StopIterationfib = Fibonacci(7)iterator = iter(fib)for x in iterator:print(x)
Get hands-on with 1400+ tech skills courses.