I am making a basic calculator using argparse. I basically accept some numbers from the command line followed by either add,subtract,multiply,or divide action. I only have the adding part right now. Here is my code:
import argparse
from sys import argv
def get_args(args):
parser = argparse.ArgumentParser(description = 'Calculator')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--add',dest = 'sum',help = "Adds the set of numbers")
parser.add_argument('--subtract',
help = "Subtracts the set of numbers")
args = parser.parse_args()
def main(argv):
args = get_args(argv[0:])
if args.a or args.add:
return sum(args)
if __name__ == "__main__":main(argv)
However, when i input:
calculator.py 1 2 3 --add
it gives me the error: expected one argument and also, in python there is a function sum(), but is there a subtract,multiply,and divide function as well? If not, then how do i return the sum without using the sum() function. I assume you would use a for loop to cycle through all the numbers?
So using the for loop would be something like this:
def main:
args = get_args(argv[0:])
if args.a or args.add:
for row in argv[1:]:
sum = sum + argv[row]
print sum