Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Behavior of statistics.mean
#5
In real world applications, you could use itertools.takewhile.
The itertools module is written in C, so it's faster than Python code.

from itertools import takewhile


def sum_user_input():

    def ask():
        while True:
            user_input = input("Enter a number: ")

            try:
                number = int(user_input)
            except ValueError:
                print(user_input, "is not a number")
                continue

            yield number

    return sum(takewhile(lambda x: x != 0, ask()))


result = sum_user_input()
print(result)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
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,177 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