Sum numbers from the command line

Easysys Module Programs · 2 of 20

Write a Python program that adds the numbers passed as arguments.

Advertisement
Output
19.5

Approach

  1. Convert each argument with float().

Solution

Python
import subprocessimport sys with open("add.py", "w") as f:    f.write("import sys\nprint(sum(float(a) for a in sys.argv[1:]))\n")out = subprocess.run([sys.executable, "add.py", "10", "2.5", "7"], capture_output=True, text=True)print(out.stdout.strip())
Output
19.5

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