Jan-29-2024, 09:18 AM
Oops! It seems like Python's Bug.
Let's show the code again.
Let's show the code again.
# unfoldr: the co-recursive iterator class unfoldr: def __init__(self, f, seed): self.f = f self.seed = seed def __iter__(self): return self def __next__(self): match self.f(self.seed): case a, b: self.seed = b return a case None: raise StopIterationNow, let's see the code showing the weird behavior again.
# Q: You input numbers. If you input 0, it means stopping input. Write a program showing the average you have inputted. def buzz(): from statistics import mean def foo(x): i = int(input()) return None if i == 0 else (i, x + 1) print(mean(unfoldr(foo, 0)))My Xubuntu has Python 3.10, 3.11, 3.12. I would show the comparison.
# On Python 3.12.1 >>> buzz() 1 2 3 4 5 6 7 8 9 10 0 # the first 0 0 # the second 0 -> that makes buzz finished and return the result 5.5
# On Python 3.11.7 >>> buzz() 1 2 3 4 5 6 7 8 9 10 0 # the first 0 0 # the second 0 -> that makes buzz finished and return the result 5.5
# On Python 3.10.12 buzz() 1 2 3 4 5 6 7 8 9 10 0 # This stops input as I expected 5.5This means, since Python 3.11, some "change" in Python causes its strange behavior.