I created a simple basic calculator program using python class

    1
    0

    12th September 2024 | 10 Views | 0 Likes

    Info: This Creation is monetized via ads and affiliate links. We may earn from promoting them.

    Toggle

    Hello Everyone,

    We all are likely aware that python is one of the most popular language in the world. In python, we can create different programs to do a lot of things like a calculator, game and a lot of other things. Here, I am going to create a simple basic calculator today using python class below.

    class Calculator:
        "Simple Calculation Program"
    
        def __init__(self, input1, input2):
            self.input1 = input1
            self.input2 = input2
            self.add = input1 + input2
            self.diff = abs(input1 - input2)
            self.cross = input1 * input2
            self.divide = round(input1 / input2, 2) if input1 > input2 else round(input2 / input1, 2)
    
        def get_result(self, opt):
            if opt == "+":
                print(f"The sum of {self.input1} and {self.input2} is {self.add}.")
            if opt == "-":
                print(f"The difference of {self.input1} and {self.input2} is {self.diff}.")
            if opt == "*":
                print(f"The product of {self.input1} and {self.input2} is {self.cross}.")
            if opt == "/":
                if self.input1>self.input2:
                    print(f"The division of {self.input1} by {self.input2} is {self.divide}.")
                else:
                    print(f"The division of {self.input2} by {self.input1} is {self.divide}.")
    
    
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))
    calculation_method = input("Enter the symbolic calculation method '+' or '-' or '*' or '/': ")
    myCalc = Calculator(num1, num2)
    myCalc.get_result(calculation_method)

    You may also like