Problem

Python

Write a simple program named sum_via_eval.py that takes in an arbitrary-size list of input numbers from the command-line, and prints out the sum of them on the terminal which is computed using Python’s eval() function. The program output should look like the following,

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

Solution

Python
import sys
print( 'The sum of {} is {}'.format( ' '.join(sys.argv[1:]) , eval('+'.join(sys.argv[1:]) ) ) )

Here is the Bash output,

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

Comments