简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM