def fib_gen():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
print(next(fib_gen()))
print(next(fib_gen()))
print(next(fib_gen()))
print(next(fib_gen()))
Output: 0
0
0
0
I am trying to create an infinite Fibonacci generator in python. Please help ... Where am I doing wrong ?
Each call to fib_gen() creates a new generator that is in initial state. Try assigning the return value of fib_gen() to a variable and calling next() on that same variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With