Problem

Python

Write a simple program named sum.py, that takes in an arbitrary-size list of input floats from the command-line, and prints out the sum of them on the terminal with the following message,

$ python sum.py 1 2 1 23
The sum of 1 2 1 23 is 27.0

Solution

Python
import sys
print( 'The sum of {} is {}'.format( ' '.join(sys.argv[1:]) , sum([float(x) for x in sys.argv[1:]]) )

Here is the Bash output,

$ python sum.py 1 2 1 23
The sum of 1 2 1 23 is 27.0

Comments