Sorry you didn't get any traction on this. While I haven't looked at the code closely, my immediate thought when you described your problem was that you were probably running in to some sort of iterator/generator behaviour or perhaps memoization/cacheing.
If it helps, I suspect the issue you were finding stems from the fact that a generator in python, is a special type of object which returns one result at a time from an iterator - the secret sauce here is the yield statement in the function definition, as opposed to using the more familiar return. This means that every time you call the function, you get a different result from last time. Take a look here for more info: https://towardsdatascience.com/6-examples-to-master-python-generators-28f4c614ed45
But one of the toy examples you can try to satisfy yourself of this is to define a generator (as in that link):
# Define a generator (the use of yield is what makes it 'special')
def mygenerator(n):
for i in range(1, n, 2):
yield i**3
# Assign it to a new object:
gen = mygenerator(10)
# Manually step through the generator using the keyword 'next'
>>> next(gen)
1
>>> next(gen)
27
>>> next(gen)
125
>>> next(gen)
343
>>> next(gen)
729
>>> next(gen)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Generators cannot be 're-iterated' without being re-made, so you will get a StopIteration exception when you try to use next() at the end of the generator.
Hopefully you can see how here you get a different result each time you call the function (for a given instance of a generator), and maybe this was some useful background info in any case.
Generators are a pretty advanced feature of python though, so don't feel put out if they don't seem immediately very intuitive (I still struggle with them half the time).