Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Behavior of statistics.mean
#2
Oops! It seems like Python's Bug.

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 StopIteration
Now, 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.5
This means, since Python 3.11, some "change" in Python causes its strange behavior.
Reply


Messages In This Thread
Behavior of statistics.mean - by cametan - Jan-29-2024, 06:51 AM
RE: Behavior of statistics.mean - by cametan - Jan-29-2024, 09:18 AM
RE: Behavior of statistics.mean - by Gribouillis - Jan-29-2024, 09:33 AM
RE: Behavior of statistics.mean - by cametan - Jan-29-2024, 11:27 AM
RE: Behavior of statistics.mean - by DeaD_EyE - Jan-30-2024, 09:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple statistics with range function Pythonlearner2019 2 2,211 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020