1

I'm trying to write a method in the Fraction class to multiply all the denominators of each fraction together. I'm try to do this by multiplying each number by a variable called CD, and I want CD to initially be 1. How can I set this to be 1, but only the first time? I tried defining in several parts of the class, but nothing has worked.

class Fraction(object):

    #define CD = 1 somewhere    

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

    def findCD(self):
        CD *= self.denominator
        return CD

fractions = [Fraction(1,2), Fraction(1,4), Fraction(9,20), 
Fraction(5,6), Fraction(3,8), Fraction(2,9)]

for i in range(0, len(fractions)):
     fractions[i].findCD()

I typically run into a problem where CD is always the value of 1, thus returning the exact values of the corresponding denominator. It's necessary that CD is in this class because it is used throughout.

Evan Hsueh
  • 139
  • 1
  • 2
  • 9

2 Answers2

0

Do it on class level using self: Initialize CD within __init__ and then return self.CD rather than CD:

class Fraction(object):
    def __init__(self, numerator, denominator):
        self.CD = 1 # do it here, add one more parameter, if needed
        self.numerator = numerator
        self.denominator = denominator

    def findCD(self):
        self.CD *= self.denominator
        return self.CD

fractions = [Fraction(1,2), Fraction(1,4), Fraction(9,20),
             Fraction(5,6), Fraction(3,8), Fraction(2,9)]

result = [f.findCD() for f in fractions]
print(result)

# Or, to output updated CD
r = [f.CD for f in fractions]
print(r)
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • This still resets to 1 after calling it in the loop. I would like the variable to permanently change after multiplying. – Evan Hsueh Jun 29 '18 at 03:06
  • Try outputing `print(fractions[i].findCD())` and you see values change - it has to be `fractions[i].CD` after `fractions[i].findCD()` is called. Just checked on my end - works. – dmitryro Jun 29 '18 at 03:11
  • Can you post all the code you ran.? Sorry, I'm having trouble implementing the solution into my program. – Evan Hsueh Jun 29 '18 at 03:21
  • So the output of the above code is `[2, 4, 20, 6, 8, 9]` and `[2, 4, 20, 6, 8, 9]` – dmitryro Jun 29 '18 at 03:23
0

Rather than passing numerator and Denominator to init you can pass it to findCD. define self.CD inside init and override it in findCD so that self.CD is initialized to 1 only when you create a class.

class Fraction(object):
    def __init__(self):
        self.CD = 1

    def findCD(self, numerator, denominator):
        self.CD = self.CD * denominator
        return(self.CD)

fractions = [(1,2), (1,4), (9,20), (5,6), (3,8), (2,9)]
fraction = Fraction()
for i in fractions:
    print(fraction.findCD(i[0], i[1]))

Output

2
8
160
960
7680
69120

The answer is specific to what you are trying to achieve. If you want to know specifically about "how to set a variable only once?" you can go through this link

Hope it helps!

mihir shanvir
  • 193
  • 1
  • 1
  • 13