简体   繁体   English

UnboundLocalError:分配问题之前引用的局部变量

[英]UnboundLocalError: local variable referenced before assignment issue

class Factor:
def __init__(self, a, b, c):                                        
    self.a = a
    self.b = b
    self.c = c

def commonFactor(self):                                             
    global cfa                                                      
    cfa = self.a                                                    
    cfb = self.b
    while cfb:                                                      
        cfa, cfb = cfb, cfa % cfb                                   
    return cfa                                                      


def simplifyIntegers(self):                                         
    self.a = int(self.a / cfa)                                      
    self.b = int(self.b / cfa)
    self.c = int(self.c / cfa)
    return self.c                                                   


def coefficients(self):                                             
    if self.a == 1:                                                 
        coe1 = 1
        coe2 = 1
    else:                                                           
        coe1 = self.a
        coe2 = 1
    return self.coe1                                                


def getFactors(self):                                                
    positivec = abs(self.c)
    global result                                                   
    result = set()
    for i in range(1, int(positivec ** 0.5) + 1):
        div, mod = divmod(positivec, i)
        if mod == 0:
            result |= {i, div}
    return result                                                   


def numbers(self):                                                 
    if self.c < 0:                                                  
        poslist = [int(x) for x in result]                          
        neglist = [-(x) for x in poslist]
        numpos = poslist[0]                                         
        numneg = neglist[-1]                                       
        for i in poslist:                                           
            number = numpos + numneg                                
            poslist.remove(numpos)                                  
            neglist.remove(numneg)
            if number == self.b:                                    
                num1 = numpos
                num2 = numneg
                return num1                                         
            elif len(poslist) > 0:                                   
                numpos = poslist[0]
                numneg = neglist[-1]
            else:                                                   
                print("This equation can not be fully factored.")
    if self.c > 0:                                                   
        poslist1 = [int(x) for x in result]                         
        poslist2 = [int(x) for x in result]
        neglist1 = [-(x) for x in poslist1]
        neglist2 = [-(x) for x in poslist1]
        numpos1 = poslist1[0]
        numpos2 = poslist2[-1]
        numneg1 = neglist1[0]
        numneg2 = neglist2[-1]
        for i in poslist1:                                          
            number = numpos1 + numpos2
            poslist1.remove(numpos1)
            poslist2.remove(numpos2)
            if number == self.b:
                num1 = numpos1
                num2 = numpos2
                return num1
            elif len(poslist1) > 0:
                numpos1 = poslist1[0]
                numpos2 = poslist2[-1]
            else:
                print("This equation can not be factored.")    
        for i in neglist1:                                          
            number = numneg1 + numneg2
            neglist1.remove(numneg1)
            neglist2.remove(numneg2)
            if number == self.b:
                num1 = numneg1
                num2 = numneg2
                return num1
            elif len(neglist1) > 0:
                numpos1 = neglist1[0]
                numpos2 = neglist2[-1]
            else:
                print("This equation can not be factored.")

def factoredForm(self):                                             
    cfa = str(cfa)
    coe1 = str(coe1)
    num1 = str(num1)
    coe2 = str(coe2)
    num2 = str(num2)
    equation = (cfa,"(",coe1,"x + ",num1,")(",coe2,"x + ",num2,")")
    return equation


a = input("What is A?")                                                 
a = int(a)
b = input("What is B?")
b = int(b)
c = input("What is C?")
c = int(c)

e = Factor(a,b,c)                                                       
print(e.factoredForm())

I keep getting this error- 我不断收到此错误-

UnboundLocalError: local variable 'cfa' referenced before assignment

I have looked at quite a bit of things talking about how to fix it, but none of those seemed to have offered something to fix this. 我看过很多有关如何解决此问题的内容,但是似乎都没有提供解决此问题的方法。 I have making the variables global, but that still doesn't work and anything else didn't work any better. 我已经将变量设置为全局变量,但这仍然行不通,并且其他任何操作都没有更好。 This is my program I made to factor quadratics if you need to know what it is doing. 如果您需要了解二次函数,这是我编写的用于二次幂分解的程序。 Thanks to anyone that can help. 感谢任何可以提供帮助的人。

Here it looks like you are trying to create a local cfa which is a str version of the global cfa . 在这里看起来您正在尝试创建本地cfa ,它是全局cfa的str版本。

def factoredForm(self):                                             
    cfa = str(cfa)

You can't mix both types of access in the same scope. 您不能在同一范围内混合使用两种类型的访问权限。 You should use a different name for the local variable. 您应该对本地变量使用其他名称。

Alternatively you could write the function like this 或者,您可以这样编写函数

def factoredForm(self):
    return map(str, (cfa, "(", coe1, "x + " ,num1, ")(", coe2, "x + " ,num2 ,")"))

These statements: 这些陈述:

cfa = str(cfa)
coe1 = str(coe1)
num1 = str(num1)
coe2 = str(coe2)
num2 = str(num2)

suggest that you want all of these variables as instance variables (not globals). 建议您将所有这些变量都作为实例变量(而不是全局变量)。 I think you have find each use and change the way you access them. 我认为您已经找到了每种用法,并更改了访问它们的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 另一个UnboundLocalError:在赋值问题之前引用的局部变量 - Another UnboundLocalError: local variable referenced before assignment Issue Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment Python基本问题(UnboundLocalError:分配前已引用局部变量“ normal”) - Basic Python Issue (UnboundLocalError: local variable 'normal' referenced before assignment) python中的怪异问题:UnboundLocalError:分配前引用了局部变量“ y” - weird issue in python: UnboundLocalError: local variable 'y' referenced before assignment UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM