reduce_rational(self):method to reduce the calculated sum, difference, product and quotient.

Here's what I have and it works:

def gcd(bigger, smaller): ''' compute the greatest common divisor of two positive integers ''' # print(' in gcd ') if not bigger > smaller : bigger, smaller = smaller, bigger while smaller != 0: remainder = bigger % smaller # print('gcd calc, big:{}, small:{}, rem:{}' \ # .format(bigger, smaller, remainder)) bigger, smaller = smaller, remainder return bigger def lcm(a, B)/>: ''' calculate the least common multiple of two positive integers ''' # print(' in lcm ') return (a * B)/> // gcd(a, B)/> def division(a, B)/>: ''' calculate the least common multiple of two positive integers ''' # print(' in lcm ') return (a * B)/> // gcd(a, B)/> def multiplication(a, B)/>: ''' calculate the least common multiple of two positive integers ''' # print(' in multiplication ') return (a // B)/>; # ----------------------------------class------------------------------------- class Rational(object): ''' rational with numerator and denominator. Denominator defaults to 1 ''' def __init__(self, numer, denom = 1): # print(' in constructor ') self.numer = numer self.denom = denom def __str__(self): ''' string representation for printing ''' # print(' in str ') return str(self.numer) + '/' + str(self.denom) def __repr__(self): ''' used in the interpreter. Call __str__ for now ''' print(' in repr ') return self.__str__() def __add__(self, param_Rational): ''' add two rationals ''' if type(param_Rational) == int: param_Rational = Rational(param_Rational) if type(param_Rational) == Rational: # find the lcm the_lcm = lcm(self.denom, param_Rational.denom) # multiply each numerator by the lcm, then add numerator_sum = the_lcm * self.numer / self.denom + \ the_lcm * param_Rational.numer / param_Rational.denom return Rational(int(numerator_sum), the_lcm) else: print("Wrong type in addition method.") raise(TypeError) def __sub__(self, param_Rational): ''' subtract two rationals ''' # print(' in add ') # find the lcm the_lcm = lcm(self.denom, param_Rational.denom) # multiply each numerator by the lcm, then add numerator_sum = the_lcm * self.numer / self.denom - \ the_lcm * param_Rational.numer / param_Rational.denom return Rational(int(numerator_sum), the_lcm) def __mul__(self, param_Rational): ''' multiply two rationals ''' if type(param_Rational) == int: param_Rational = Rational(param_Rational) if type(param_Rational) == Rational: # multiply numerator numerator_mul = self.numer * param_Rational.numer # multiply denominator denominator_mul = self.denom * param_Rational.denom return Rational(int(numerator_mul), int(denominator_mul)) else: print("Wrong type in multiplication method.") raise(TypeError) def __truediv__(self, param_Rational): ''' divide two rationals ''' if type(param_Rational) == int: param_Rational = Rational(param_Rational) if type(param_Rational) == Rational: # multiply numerator numerator_mul = self.numer * param_Rational.denom # multiply denominator denominator_mul = self.denom * param_Rational.numer return Rational(int(numerator_mul), int(denominator_mul)) else: print("Wrong type in division method.") raise(TypeError) def reduce_rational(self): ''' return the reduced fraction value as a rational ''' # find the gcd and divide numerator and denominator by it the_gcd = gcd(self.numer, self.denom) return Rational(self.numer // the_gcd, self.denom // the_gcd) def __eq__(self, param_Rational): ''' compare two rationals for equalit and return a boolean ''' reduced_self = self.reduce_rational() reduced_param = param_Rational.reduce_rational() return reduced_self.numer == reduced_param.numer and \ reduced_self.denom == reduced_param.denom # -----------------------------------main------------------------------------- def main(): r1 = Rational(1, 2) r2 = Rational(1, 4) print("First Rational Number:", r1.__str__()) print("Second Rational Number:", r2.__str__()) print("Sum:", r1.__add__(r2)) print("Difference:", r1.__sub__(r2)) print("Product:", r1.__mul__(r2)) print("Quotient:", r1.__truediv__(r2)) if __name__ == "__main__": main()

This is the output!

First Rational Number: 1/2 Second Rational Number: 1/4 Sum: 3/4 Difference: 1/4 Product: 1/8 Quotient: 4/2

But how do I get it reduced?

I tried:

def main(): r1 = Rational(1, 2) r2 = Rational(1, 4) print("First Rational Number:", Rational.reduce_rational(r1.__str__())) print("Second Rational Number:", Rational.reduce_rational(r2.__str__())) print("Sum:", Rational.reduce_rational(r1.__add__(r2))) print("Difference:", Rational.reduce_rational(r1.__sub__(r2))) print("Product:", Rational.reduce_rational(r1.__mul__(r2))) print("Quotient:", Rational.reduce_rational(r1.__truediv__(r2)))

... but got an AttributeError.

Please help. I'm kind of new to classes in Python.