728x90
반응형
num1 = int(input("first num : "))
opt = input("operator : ")
num2 = int(input("second num: "))

class FourCal():
    def __init__(self, num1, num2):    # Constructor: object initialize function
        self.num1 = num1
        self.num2 = num2
    def setdata(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
    def add(self):
        result = self.num1 + self.num2
        return result
    def sub(self):
        result = self.num1 - self.num2
        return result
    def mul(self):
        result = self.num1 * self.num2
        return result
    def div(self):
        if self.num2 == 0:
            return 0
        else:
            return self.num1 / self.num2

 #Class Inheritance
class MoreFourCal(FourCal):
    def pow(self):
        result = self.num1 ** self.num2
        return result

#a = FourCal(num1, num2)     # num1, num2 transfer
a = MoreFourCal(num1, num2)

if (opt == "+"):
    # add = opt
    a.setdata(num1, num2)
    print(a.add())
elif (opt == "-"):
    # sub = opt
    a.setdata(num1, num2)
    print(a.sub())
elif (opt == "*"):
    # mul = opt
    a.setdata(num1, num2)
    print(a.mul())
elif (opt == "/"):
    # div = opt
    a.setdata(num1, num2)
    print(a.div())
elif (opt == "**"):
    a.setdata(num1, num2)
    print(a.pow())
728x90
반응형

'Language > Python' 카테고리의 다른 글

To change the Python version & Interpreter for VSCODE  (0) 2022.09.08
How to use Numpy  (0) 2022.09.06
Jump to Python_Source code  (0) 2022.08.26
Python_11. Collection Management  (0) 2022.08.03
Python_10. Dictionary and Set  (0) 2022.08.01

+ Recent posts