Fixing Unbound local error: local variable reference before assignment

34
February 22, 2021, at 07:10 AM

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
Answer 1

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
            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
        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
READ ALSO
Finding Javafx node sizes

Finding Javafx node sizes

I read somewhere that thegetWidth() function only works when the node itself is properly initiated

32
NodeJs Telegram bot on VPS, need help switching to webhook

NodeJs Telegram bot on VPS, need help switching to webhook

please, help me understand how to switch my Telegram bot from polling to webhookI have a VPS server where my bot is deployed

27
I have problem with IE11 in Grid. I uploaded my code. It work fine in chrome and firefox [closed]

I have problem with IE11 in Grid. I uploaded my code. It work fine in chrome and firefox [closed]

Want to improve this question? Add details and clarify the problem by editing this post

29