0

Code for add function:

def __add__(self, rightSide):
        """
        Purpose: Adds two fractions together to get the sum
        :param rightSide: Placeholder for second fraction to be calculated with original fraction
        :return: None
        """
        numer = self.__numerator * rightSide.getDenominator() + self.__denominator * rightSide.getNumerator()
        denom = self.__denominator * rightSide.getDenominator()
        new_frac = Fraction(numer, denom)
        print(new_frac)

Driver code:

print(f'{frac_a} + {frac_b} = ', frac_a + frac_b)

Output:

16/15
2/5 + 2/3 =  None

When I have frac_a at the end of the formatting code, it will print 2/5 after the equal sign. But when I add the operator and frac_b, it outputs what js shown.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

1 Answers1

0

You should return the value after adding

def __add__(self, rightSide):
        """
        Purpose: Adds two fractions together to get the sum
        :param rightSide: Placeholder for second fraction to be calculated with original fraction
        :return: None
        """
        numer = self.__numerator * rightSide.getDenominator() + self.__denominator * rightSide.getNumerator()
        denom = self.__denominator * rightSide.getDenominator()
        new_frac = Fraction(numer, denom)
        print(new_frac)
        return new_frac

Otherwise, it will return None by default as your output.

Đào Minh Hạt
  • 2,742
  • 16
  • 20