0

I am to write a class 'rational' with a set of instance methods that when initialized, like for instance r = Rational(-49, 168) has a set of methods which finds the highest common denominator between these two and returns the arguments divided by this hcd.

This works just fine for positive arguments, as well as when the left argument is positive. When the right argument, the argument for denominator is negative, however, self.denominator disappears and I get False if i try:

r = Rational(49, -168)
print(r.get_num() == -7 and r.get_denom() == 24)
class Rational:

    def __init__(self, numerator, denominator=1):
        self.numerator = numerator
        self.denominator = denominator

    def cd_numerators(self):
        list_cd_numerators = []
        for i in range(1, abs(self.numerator+1)):
            if abs(self.numerator) % i == 0:
                list_cd_numerators.append(i)
        return list_cd_numerators

    def cd_denominators(self):
        list_cd_denominators = []
        for i in range(1, abs(self.denominator+1)):
            if abs(self.denominator) % i == 0:
                list_cd_denominators.append(i)
        return list_cd_denominators

    def highest_common_denominator(self, a, b):
        highest_common_denominator1 = 1
        for i in a:
            if i in b:
                highest_common_denominator1 = i
        return highest_common_denominator1

    def get_num(self):
        common_denominator = self.highest_common_denominator(self.cd_numerators(), self.cd_denominators())
        numerator = self.numerator / common_denominator
        print(numerator)
        return numerator

    def get_denom(self):
        common_denominator = self.highest_common_denominator(self.cd_numerators(), self.cd_denominators())
        denominator = self.denominator / common_denominator
        print(denominator)
        return denominator

As you can see the code for get_denom(self) and get_num(self) is precisely the same, so I cannot understand where I'm going wrong.

azro
  • 53,056
  • 7
  • 34
  • 70
Osmani
  • 37
  • 5
  • 1
    As `r.get_num() == -7` is False, it doesn't evaluate the other operand of `and`, as it's useless, we already know the whole would be False, that is called short-circuiting, to get faster code. Same with `or` : if first is True, don't evaluate the other operand – azro Dec 04 '21 at 10:34
  • A great many thanks! – Osmani Dec 04 '21 at 10:36
  • this is one of many reasons why having functions that do two things is a bad idea ( e.g. get_denom both calculates the denominator and prints the denominator) – JeffUK Dec 04 '21 at 10:36
  • Why do you expect the numerator to be negative when you passed a positive argument into `__init__`? When I run the code, `get_num` returns 7.0 and `get_denom` returns -24.0 – The_spider Dec 04 '21 at 10:40
  • These were given to us by our uni. We are supposed to make a number of statements print True by writing this class with methods. This was 1 of 8 statements which were supposed to print True. – Osmani Dec 04 '21 at 10:47

0 Answers0