Reputation: 81
I am trying to create a simple script that will accept age as an argument to determine the appropriate fee for admission. (< 6 = Free; >= 7 and <=14 is $10; >=15 and <=59 is $15; >= 60 is $5)
I've managed to get the script to work if I input the age at the age = __ within the script, but I want to be able to pass in age as an argument in the Arguments box of the Run Script window. I think I should be able to use sys.argv but I haven't been able to figure out how. I suspect it has to do with some sort of disagreement between what sys.argv wants and what I'm providing--maybe a mismatch with strings and integers?
Any ideas?
# Enter age
age = 5
if age < 7:
print ("Free Admission")
elif age < 15:
print ("Admission: $10")
elif age < 60:
print ("Admission: $15")
else:
print ("Admission: $5")
Upvotes: 4
Views: 85
Reputation: 24812
# here you import `sys` that enables access to `sys.argv`
import sys
# then you define a function that will embed your algorithm
def admission_fee(age):
age = int(age)
if age < 7:
print ("Free Admission")
elif age < 15:
print ("Admission: $10")
elif age < 60:
print ("Admission: $15")
else:
print ("Admission: $5")
# if you call this python module from the command line, you're in `__main__` context
# so if you want to reuse your module from another code, you can import it!
if __name__ == "__main__":
# here you use the first argument you give to your code,
# sys.argv[0] being the name of the python module
admission_fee(sys.argv[1])
As Martijn says, you can use argparse
module or I now prefer to use the docopt
module which makes it easier and nicer to implement!
As an example of docopt use:
"""Usage: calcul_fee.py [-h] AGE
Gets the admission fee given the AGE
Arguments:
AGE age of the person
Options:
-h --help"""
from docopt import docopt
def admission_fee(age):
age = int(age)
if age < 7:
print ("Free Admission")
elif age < 15:
print ("Admission: $10")
elif age < 60:
print ("Admission: $15")
else:
print ("Admission: $5")
if __name__ == '__main__':
arguments = docopt(__doc__)
admission_fee(arguments.AGE)
Upvotes: 1
Reputation: 1122002
Sure, that's absolutely what you can do with sys.argv
; just take into account that sys.argv[0]
is the script name, and all values in it are strings:
import sys
if len(sys.argv) > 1:
try:
age = int(sys.argv[1])
except ValueError:
print('Please give an age as an integer')
sys.exit(1)
else:
print('Please give an age as a command line argument')
sys.exit(1)
Python comes with the argparse
module which makes parsing sys.argv
arguments a little easier still:
import argparse
parser = argparse.ArgumentParser('Admission fee determination')
parser.add_argument('age', type=int, help='the age of the visitor')
args = parser.parse_args()
age = args.age
where age
is already an integer. As an added bonus, your script is given a help text too:
$ python yourscript.py -h
usage: Admission fee determination [-h] age
positional arguments:
age the age of the visitor
optional arguments:
-h, --help show this help message and exit
and automatic error feedback if age
is not given or not an integer.
Upvotes: 4