Creating a class in python with several methods

I have started learning Python and want to create a class which takes four numbers as input and does division, mutiplication, addition and subtraction. The results should be printed. I made a class, but i don’t know how to include different methods.

class fraction: 

def __init__(self, numerator1, denominator1, numerator2, denominator2): 
    
    self.numerator1 = numerator1
    self.denominator1 = denominator1
    self.numerator2 = numerator2
    self.denominator2 = denominator2 
    
    
    self.addition_numerator=numerator1*denominator2+numerator2*denominator1
    self.addition_denominator=denominator1*denominator2
    
    
    self.subtraction_numerator=numerator1*denominator2-numerator2*denominator1 
    self.subtraction_denominator=denominator1*denominator2
    
   
    self.multiplication_numerator=numerator1*numerator2 
    self.multiplication_denominator=denominator1*denominator2
    
    
    self.division_numerator=numerator1*denominator2 
    self.division_denominator=denominator1*numerator2


    user_fraction=fraction(int(input("Enter the first numerator: ")), int(input("Enter the first denominator: ")),int(input("Enter the second numerator: ")), int(input("Enter the second denominator: ")))       


    print(user_fraction.__dict__)

Answer

Replace num1 and num2 with num3 and num4.

class fraction:
      def __init__(self,num1,num2,num3,num4):
       self.num1=num1
       self.num2=num2
       self.num3=num3
       self.num4=num4
      def addition(self):
        print(self.num1+self.num2)
        return self.num1+self.num2
      def sub(self):
         print(self.num1+self.num2)
         return self.num1+self.num2
      def div(self):
         print(self.num1/self.num2)
         return self.num1/self.num2
      def mul(self):
         print(self.num1*self.num2)
         return self.num1*self.num2
      def reminder(self):
         print(self.num1%self.num2)
         return self.num1%self.num2


     if __name__ =="__main__":
       object=fraction(1,2,3,4)
       object.addition()
       object.reminder()

Leave a Reply

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