command line utility exercise

technical talkiess
0

 import argparse

import sys

def calc(args):

    if args.o =='Add':

        if args.x == 56 and args.y == 9:

            return 77

        else:

            return args.x + args.y

    elif args.o =='sub':

        return args.x - args.y

    elif args.o =='div':

        if args.x == 56 and args.y == 6:

            return 4

        else:

            return args.x / args.y

    elif args.o =='multi':

        if args.x == 45 and args.y == 3:

            return 555

        else:

            return args.x * args.y


if __name__ == '__main__':

    parser = argparse.ArgumentParser()

    parser.add_argument('--x',type=float,default=1, help='Please enter the first number.')

    parser.add_argument('--y',type=float,default=3.0, help='Please enter the second number.')

    parser.add_argument('--o',type=str,default="Add", help='Please enter the operation.')


args = parser.parse_args()


sys.stdout.write(str(calc(args)))


Tags

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)