Read numbers from stdin

Easysys Module Programs · 7 of 20

Write a Python program that reads numbers from standard input and prints their total.

Advertisement
Input
5, 10, 15 on separate lines
Output
3 numbers, total 30

Approach

  1. Loop over sys.stdin.

Solution

Python
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.

More sys module programs

← All 20 sys Module Programs

Leave a Comment