Jan-30-2024, 09:20 PM
In real world applications, you could use
The
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!
All humans together. We don't need politicians!