Python Practice › sys Module Programs › Program 7
Read numbers from stdin
Write a Python program that reads numbers from standard input and prints their total.
Input
5, 10, 15 on separate lines
Output
3 numbers, total 30
Approach
- Loop over
sys.stdin.
Solution
import sys numbers = [int(line) for line in sys.stdin if line.strip()]print(len(numbers), "numbers, total", sum(numbers))Outputtyped input: 5, 10, 15
3 numbers, total 30
Learn the concept
New to this topic? Read the lesson, then come back.