how to make a calculator in python using object oriented programming

Solutions on MaxInterview for how to make a calculator in python using object oriented programming by the best coders in the world

showing results for - "how to make a calculator in python using object oriented programming"
Bethanie
14 Oct 2016
1please subscribe my channel - https://bit.ly/2Me2CfB
2
3class Calculator:
4    def sum(self):
5        return self.num1 + self.num2
6    def difference(self):
7        return self.num1 - self.num2
8    def multiply(self):
9        return self.num1 * self.num2
10    def divide(self):
11        return self.num1 / self.num2
12
13c = Calculator()
14c.num1 = int(input("ENTER THE NUMBER 1 : "))
15c.num2 = int(input("ENTER THE NUMBER 2 : "))
16
17print(c.sum())
18
19# print(c.difference())
20
21# print(c.multiply())
22
23# try:
24#    print(c.divide())
25# except ZeroDivisionError:
26#    print("Unable to divide by zero")
Lisa
23 Oct 2020
1please subscribe my channel - https://bit.ly/2Me2CfB
2
3# object oriented programming ( OOPS ) code : 
4class Calculator:
5    def sum(self):
6        return self.num1 + self.num2
7    def difference(self):
8        return self.num1 - self.num2
9    def multiply(self):
10        return self.num1 * self.num2
11    def divide(self):
12        return self.num1 / self.num2
13
14c=Calculator()
15c.num1 = int(input("ENTER NUMBER 1 : "))
16c.num2 = int(input("ENTER NUMBER 2 : "))
17op = input("ENTER THE OPERATION FOR NUMBERS : ")
18
19if op == '+':
20    nsum = c.sum()
21    print(nsum)
22if op == '-':
23    ndif = c.difference()
24    print(ndif)
25if op == '*':
26    nmul = c.multiply()
27    print(nmul)
28if op == '/':
29    try:
30        ndiv = c.divide()
31        print(ndiv)
32    except ZeroDivisionError:
33        print("Unable to divide with zero")
34