Calci

I have built Calci, a command line calculator for linux that performs basic operations like addition, subtraction, division and multiplication. I have used Python3 to build this command line tool. You will find that I had already made a blog post on Command line calculator earlier. Now this new post comes with some edits of the earlier one and another important thing is that I have made this open source .

To run Calci follow the steps given below:

  • git clone https://github.com/trishnaguha/Calci.git
  • python3 main.py followed by arguments

Since it is a command line tool you’ll need to provide arguments after python3 main.py . Type python3 main.py -h or python3 main.py –help to know this in details.

you can also fork Calci on github and send pull requests to add extra features or edit the existing features. You are also invited to open issue if you find any.

I am really excited about this project, Looking forward to turn this into a scientific one. 🙂

Advertisements

A Command-line Calculator using Python 3

Kushal introduced us to command-line parsing module argparse in Python3 on Wednesday in dgplug. Well now I am having the real fun with Python.

I have made a simple calculator using argparse that calculates addition, subtraction, division and multiplication. Run the code in Linux terminal as given below in the image.

The code:

import argparse

#Addition function
def add(a,b):
    val = a+b
    return val

#Subtraction function
def sub(a,b):
    val = a-b
    return val

#Division function
def div(a,b):
    val = a/b
    return val

#Multiplication function
def multi(a,b):
    val = a*b
    return val

#Main function
def Main():
    parser = argparse.ArgumentParser()

    group = parser.add_mutually_exclusive_group()
    group.add_argument("-fa","--fadd",help="Performs addition",action="store_true")
    group.add_argument("-fs","--fsub",help="Performs subtraction",action="store_true")
    group.add_argument("-fd","--fdiv",help="Performs division",action="store_true")
    group.add_argument("-fm","--fmulti",help="Performs multiplication",action="store_true")

    parser.add_argument("num1",help="Number1 to calculate",type=int)
    parser.add_argument("num2",help="Number2 to calculate",type=int)

    args = parser.parse_args()
    
#Optional arguments 
    if args.fadd:
        print("The addition result of {} and {} is {}".format(args.num1,args.num2,(add(args.num1,args.num2))))
    elif args.fsub:
        print("The subtraction result of {} and {} is {}".format(args.num1,args.num2,(sub(args.num1,args.num2))))
    elif args.fdiv:
        print("The division result of {} and {} is {}".format(args.num1,args.num2,(div(args.num1,args.num2))))
    elif args.fmulti:
        print("The multiplication result of {} and {} is {}".format(args.num1,args.num2,(multi(args.num1,args.num2))))
    else:
        print("Error:Requires an argument to perform an action")

if __name__ == '__main__':
    Main()

This works like :

Screenshot from 2015-08-07 02:00:12

Leave comments for modification of the code 🙂