Created
June 11, 2021 12:57
-
-
Save rajmayank/2d8be009037c5c36944d60cee14f8592 to your computer and use it in GitHub Desktop.
Python-Iterators-FibonacciTill20-IV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def FibonacciTill20(): | |
n1, n2 = 0, 1 | |
max_num = 20 | |
while True: | |
n1, n2 = n2, (n1 + n2) | |
if n2 <= max_num: | |
yield n2 | |
else: | |
raise StopIteration | |
fibonacci_nums = FibonacciTill20() | |
print(fibonacci_nums.__iter__()) # <generator object FibonacciTill20 at 0x10b3da190> | |
print(fibonacci_nums.__next__()) # 1 | |
print(fibonacci_nums.__next__()) # 2 | |
print(fibonacci_nums.__next__()) # 3 | |
print(fibonacci_nums.__next__()) # 5 | |
print(fibonacci_nums.__next__()) # 8 | |
print(fibonacci_nums.__next__()) # 13 | |
print(fibonacci_nums.__next__()) # Exception: StopIteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment