def fibonacci_sequence():
a,b = 1,1
while True:
yield a
a,b = b, a+b
for i in range(10):
print(fibonacci_sequence().__next__())
I tried using this in Python 3 to print out the fibonacci series. But the program just ends up printing 1 over and over again
fibonnacci_sequence()and then you call__next__()on that new object, so of course, you only ever get the first value. As an aside, the whole point of generators is that you can iterate directly over them (they are iterators). Note also, you shouldn't call dunder methods directly, so if you do need it, usenext(some_iterator)notsome_iterator.__next__()