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.