Make a cool and Advanced Calculator in python – beginners project 2021

Spread To Friends

how to Make an Advanced Calculator with python

Today we are going to see how to make a cool and advanced calculator in python. Python is the easiest and powerful programming language nowadays. Many large companies use this language to make their tools and software. Here in this tutorial, we are going to see how to make a cool calculator in python language, it can be very useful for one who always does financial work. So let’s get started.

Project Roadmap

Basic Calculations

We are going to make a calculator where we can type the values what we need like:

1+2+3+4 and when we hit enter the result should be in our screen and this will go long until the user types q and enters.

Currency converter

The second functionality will be the currency converter, as there are lots of currencies so we will be converting some specific currency to another currency. First, we need to convert INR to USD and USD to INR and then we will be converting USD to EURO and EURO to USD and lastly MXN to USD and USD to MXN. this will be a currency converter using python.

Weight Converter

We will be converting the weight units from one to another, as there are many weight calculating units so we will just add those units that we use in daily life. We will convert through Gram, kilogram, pound for now in the future we also are adding more functionalities in this calculator. This will be a weight converter using python.

Temperature Converter

We will be converting through celsius, fahrenheit, and kelvin scales for temperature. This will be a temperature converter using python.

Time Converter

Lastly we will convert the time from hours to minutes and seconds and milliseconds.

So these would be our roadmap to doing this. This will be a time converter using python.

Let’s Code

Now we are going to code the whole program of making a fully functional calculator in Python language. 

First of all, we will make the basic calculations function in python. The input will be like if anyone wants to make an addition then the input will be 1+2+3 first we are going to make this function.

def calculate(values):
    """ The syntax is 3+2+3 or 3-2 or 3*2 or 3/2 this will be the syntaxes for calculating the values """
 
    # Global separators
    add  = values.split("+")
    sub  = values.split("-")
    mul  = values.split("*")
    div  = values.split("/")
    curr = values.split(">")
    wei  = values.split(">>")
    temp = values.split("=")
    tm   = values.split("~")
    ##########
 
    result = 0
    if len(add) != 1:
        for i in add:
            newval = int(i)
            result = result+newval
        return result
 
    elif len(sub) != 1:
        result = int(sub[0])-int(sub[1])
        return result
 
    elif len(mul) != 1:
        mulres = 1
        for i in mul:
            newval = int(i)
            mulres = mulres*newval
        return mulres
 
    elif len(div) != 1:
        result = int(div[0])/int(div[1])
        return result
    
    # If calculations are no valid
 
    elif len(curr) != 1:
        res = currency(values)
        return res
    elif len(wei) != 1:
        res = weight(values)
        return res
    elif len(temp) != 1:
        res = temperature(values)
        return res
    elif len(tm) != 1:
        res = time(values)
        return res
 

This is the first method named calculate and it is receiving a value which is the input value then the input value is being separated by the global separators. And getting broken in different parts. Then we declared a variable called result to 0. Then we are checking if the value that we separated contains any values or not if yes then it goes under the if statement and processes the given algorithm. 

In this way we made the calculate section but wait! What’s in the below section ? it seems like there other methods are called. 

Well, right there other methods are called, 

Redirecting to other methods

When you do not want to use the basic calculations but you want to use other calculations then we will be redirected to other methods based on which function you want to access. Which function you want to access based on the separator you used.

Function Separators

The Function separators will define which function you want to access and what conversation rates you want. Like when you want the conversation of currencies and from USD to INR then you have to define the input like this, 100d>i this means that you want to convert 100 USD to INR. Here “>” separator used. This separator defines that you want to use the currency calculator. There are other function separators.

“>”   – Currency converter

“>>” – Weight converter

“=”   – Temperature converter

“~”   – Time converter

Now we will be making the currency calculator function. 

def currency(values):
    """Currency Calculator. The syntaxes will be like 100i>d or 100i>e or 100i>m
    here, i = INR
         d = USD
         e = EURO
         m = MXN
     """
    INR_USD = 0.01373 # this is for inr to usd and usd to inr
    INR_EURO = 0.0114 # this is for inr to euro and euro to inr
    INR_MXN = 0.2757 # this is for inr to mxn and mxn to inr
 
    USD_EURO = 0.8302 # this is for usd to euro and euro to usd
    USD_MXN = 20.077 # this is for usd to mxn peso and euro to mxn peso 
 
    EURO_MXN = 24.1833 # this is for euro to mxn peso and mxn peso to euro
 
 
    result = 0
    spl = values.split(">")
    if 'i' in spl[0]:
        if 'd' in spl[1]:
            num = spl[0].split("i")
            numint = int(num[0])
            result = numint*INR_USD
            return result
 
        elif 'e' in spl[1]:
            num = spl[0].split("i")
            numint = int(num[0])
            result = numint*INR_EURO
            return result
 
        elif 'm' in spl[1]:
            num = spl[0].split("i")
            numint = int(num[0])
            result = numint*INR_MXN
            return result
 
    elif 'd' in spl[0]:
        if 'i' in spl[1]:
            num = spl[0].split("d")
            numint = int(num[0])
            result = numint/INR_USD
            return result
        elif 'e' in spl[1]:
            num = spl[0].split("d")
            numint = int(num[0])
            result = numint*USD_EURO
            return result
        elif 'm' in spl[1]:
            num = spl[0].split("d")
            numint = int(num[0])
            result = numint*USD_MXN
            return result
 
    elif 'e' in spl[0]:
        if 'i' in spl[1]:
            num = spl[0].split("e")
            numint = int(num[0])
            result = numint/INR_EURO
            return result
        elif 'd' in spl[1]:
            num = spl[0].split("e")
            numint = int(num[0])
            result = numint/USD_EURO
            return result
        elif 'm' in spl[1]:
            num = spl[0].split("e")
            numint = int(num[0])
            result = numint*EURO_MXN
            return result
 
    elif 'm' in spl[0]:
        if 'i' in spl[1]:
            num = spl[0].split("m")
            numint = int(num[0])
            result = numint/INR_MXN
            return result
        elif 'd' in spl[1]:
            num = spl[0].split("m")
            numint = int(num[0])
            result = numint/USD_MXN
            return result
        elif 'e' in spl[1]:
            num = spl[0].split("m")
            numint = int(num[0])
            result = numint/EURO_MXN
            return result

First of all, we are taking the input value as a parameter. And then we have declared the global variables which are the conversation values of the currencies. Then we started to split the values and the calculations.

Now we are pausing the process here, but we’ll cover the rest of the topics in the upcoming tutorials.  But for now, we haven’t called the calculate function yet. So the below code is to call the function and make a pipeline for upcoming functions that we will make in the future. 

def weight(values):
    """ The syntaxes will be like 100g>>ki or 100g>>p 
    here, g  = GRAMS
          ki = KILOGRAMS
          p  = POUND
    """
    pass
def temperature(values):
    """ The syntaxes will be like 100c=f or 100c=k 
    here, c = Celsius
          f = Farenheit
          k = Kelvin
    """
    pass
def time(values):
    """ The syntaxes will be like 100h~m or 100m~s or 100s~ms 
    here, h = HOUR
          m = MINUTE
          s = SECOND
          ms = MILLISECOND
    """
    pass
 
if __name__ == '__main__':
    import os
    print("\n\n\t\t\tWelcome to The Advanced Calculator - by: Sayantan Biswas ~ Technicalsayan.in\n\n")
    input("Press any key to continue: ")
 
    # for mac and linux(here, os.name is 'posix')
    if os.name == 'posix':
        os.system('clear')
    else:
        # for windows platform
        os.system('cls')
 
    print("""
    The syntax is 3+2+3 or 3-2 or 3*2 or 3/2 this will be the syntaxes for calculating the values\n\n
    Currency Calculator. The syntaxes will be like 100i>d or 100i>e or 100i>m 
    here, i = INR
         d = USD
         e = EURO
         m = MXN\n\n
    The syntaxes for weight will be like 100g>>ki or 100g>>p 
    here, g  = GRAMS
          ki = KILOGRAMS
          p  = POUND\n\n
    The syntaxes for temperature will be like 100c=f or 100c=k 
    here, c = Celsius
          f = Farenheit
          k = Kelvin\n\n
    The syntaxes for time will be like 100h~m or 100m~s or 100s~ms 
    here, h = HOUR
          m = MINUTE
          s = SECOND
          ms = MILLISECOND\n
    Press q to exit the terminal\n
    """)
    input("\nPress enter: ")
    while True:
        # for mac and linux(here, os.name is 'posix')
        if os.name == 'posix':
            os.system('clear')
        else:
        # for windows platform
            os.system('cls')
        print("\n\n\t\t\tWelcome to The Advanced Calculator - by: Sayantan Biswas ~ Technicalsayan.in\n\n")
        inp = input("Enter your values: ")
        if inp == "q":
            break
        else:
            print("The result is:",calculate(inp))
            input("\nPress enter: ")

Conclusion

So today we saw how to make a cool and advanced calculator in python, it can make your life very easy. This can also play a major role to make one’s portfolio very attractive and useful. First, we covered the basic functionalities, and then we made the currency converter for this tutorial. Stay tuned to us by clicking the below bell icon or the below subscribe button to get notifications of new posts.

see all python tutorials from here: https://www.technicalsayan.in/?s=python

see more about python3: https://docs.python.org/3/

close
Oval@3x 2

Don’t miss these updates!

We don’t spam! Read our privacy policy for more info.


Spread To Friends

Leave a Reply

Your email address will not be published. Required fields are marked *