Flag control variable
Peter Otten
__peter__ at web.de
Tue Feb 11 12:46:40 EST 2014
luke.geelen at gmail.com wrote:
> i'd like to know how to set up a flag to change a variable,
> for example, i want a simple script to combine 2 numbers,
>
>
> sum = num + another_num
> print "Now the sum of the numbers equals : ", sum
>
> how could i make it so that if i type python ./script.py 21 41
> that i get the sum of 21 and 41 ?
You seem to be looking for sys.argv which contains the script name and the
command-line arguments.
$ cat script.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print a, "+", b, "=", a + b
$ python script.py 21 41
21 + 41 = 62
The conversion to int (or float etc.) is necessary because in python
>>> "21" + "41"
'2141'
More information about the Python-list
mailing list