hannah
hannah

Reputation: 195

Fixing Unbound local error: local variable reference before assignment

I am getting an error saying "UnboundLocalError: local variable 'result_numerator' referenced before assignment" and not sure why. Any kind of help will be appreciated

def plus(self, other: "Fraction") -> "Fraction":
    if (self.denominator > other.denominator):
        if (self.denominator / other.denominator).is_integer() == True:
            multiple = self.denominator / other.denominator
            newSelfDenom = other.denominator * multiple
            newSelfNumer = other.numerator * multiple
        else:
            newSelfNumer = self.numerator * other.denominator
            newSelfDenom = self.denominator * other.denominator
            newOtherNumer = other.numerator * self.denominator
            newOtherDenom = other.denominator * self.denominator
    elif (other.denominator > self.denominator):
        if (other.denominator / self.denominator).is_integer() == True:
            multiple = other.denominator / self.denominator
            newOtherDenom = other.denominator * multiple
            newOtherNumer = other.numerator * multiple
        else:
            newSelfNumer = self.numerator * other.denominator
            newSelfDenom = self.denominator * other.denominator
            newOtherNumer = other.numerator * self.denominator
            newOtherDenom = other.denominator * self.denominator
    else:
        result_denominator = self.denominator
        result_numerator = self.numerator + other.numerator
    result = Fraction(result_numerator, result_denominator)
    return result

Upvotes: 1

Views: 204

Answers (1)

VirtualScooter
VirtualScooter

Reputation: 1888

You reference result_numerator in the one but last line of your method definition, but it is not defined when self.denominator <> other denominator. Better code:

def plus(self, other: "Fraction") -> "Fraction":
    if (self.denominator > other.denominator):
        if self.denominator % other.denominator == 0:
            multiple = self.denominator / other.denominator
            newSelfDenom = other.denominator * multiple
            newSelfNumer = other.numerator * multiple
        else:
            newSelfNumer = self.numerator * other.denominator
            newSelfDenom = self.denominator * other.denominator
            newOtherNumer = other.numerator * self.denominator
            newOtherDenom = other.denominator * self.denominator
        result_denominator = newSelfDenom
        result_numerator = newSelfNumer
    elif (other.denominator > self.denominator):
        if other.denominator % self.denominator == 0:
            multiple = other.denominator / self.denominator
            newSelfDenom = other.denominator * multiple
            newSelfNumer = other.numerator * multiple
        else:
            newSelfNumer = self.numerator * other.denominator
            newSelfDenom = self.denominator * other.denominator
            newOtherNumer = other.numerator * self.denominator
            newOtherDenom = other.denominator * self.denominator
        result_denominator = newSelfDenom
        result_numerator = newSelfNumer
    else:
        result_denominator = self.denominator
        result_numerator = self.numerator + other.numerator
    result = Fraction(result_numerator, result_denominator)
    return result

Upvotes: 1

Related Questions