#!/usr/bin/python # Sanjoy Mahajan and David MacKay # python/examples/commandline2.py # Usage = """ A python program to sum the integers from 1 to n, where n is given on the command line. $ commandline2.py 15 120 To make this file executable, $ chmod +x commandline2.py """ import sys total = 0 if (len(sys.argv)<2): print "error - usage:\n $ commandline2.py " else: for i in range(int(sys.argv[1])+1): total += i print total print """ The int function converts the string sys.argv[1] to an integer. Notice the +1 in the range function. """