简体   繁体   中英

Python - UnboundLocalError: local variable “ID” referenced before assignment

Id, conf = recognizer.predict(gray[y:y+h,x:x+w]
def hour(cn):
   for z in range(9,17):  
      if now.hour == z:
         worksheet(cn, str(z)+":00")

def identify(number):
   sht = gc.open("Test")
   wks3 = sht.worksheet("NAMES")
   b = wks3.acell('B'+str(number)).value
   a = wks3.acell('A'+str(number)).value
   if(Id == a and conf<65):
     print(Id, conf)
     Id = str(b)
     Time = time.ctime()
     hour(number)
   elif(conf>64):
     print(conf)
     Id = "Unknown"

for m in range(2,100):

     identify(m)

The above code is being used for facial recognition, I copied what I felt was necessary, it is not the entire code.

I'm trying create a function which I want to call back in a for loop

What am I doing wrong? I've been looking t this for 6 hours now, and anything I try doesn't seem to work.

I get a message back saying "UnboundLocalError: local variable 'Id' referenced before assignment"

It's impossible because I'm assigning with:

a = wks3.acell('A'+str(number)).value

So it grabs the ID number from the google spread sheet and checks if it is equaled to that, can someone tell me where I'm going wrong here?

def identify(number):
   sht = gc.open("Test")
   wks3 = sht.worksheet("NAMES")
   b = wks3.acell('B'+str(number)).value
   a = wks3.acell('A'+str(number)).value
   #because you did, Id = ? 
   if(Id == a and conf<65):
     print(Id, conf)
     Id = str(b)
     Time = time.ctime()
     hour(number)
   elif(conf>64):
     print(conf)
     Id = "Unknown"

Because you did, variable Id isn't passed as any parameter or global/local variable or as an argument to existing class.

If Id was parameter:

def identify(number,Id): 

If Id was global variable:

def identify(number):
    global Id  

If Id was local variable:

def identify(number):
    id = None # or some other data type  

And if Id was argument from some class:

some_class.Id 

In short you referenced Id before it was initialised. This is rookie mistake and there is some stuff where you can actually init a variable in if elif else statement but you need to trow a none of above logic of the rule.

if True: Id = 2; elif False: Id = 3; else: Id =0 #this is pseudocode, don't paste it in.

Also have in mind that next variable is also Unbound conf


EDIT:

Often to avoid this problem we write code like this:

def somefunction(parm1,parm2... ): 

    # global variables : description for variable stack is optional
    global var1,var2 # if needed  

    #local variables  
    var3,var4 = None;  
    var5 = 'something else'  

    #in body functions : functions inside functions or just general program functions 
    def a(... ): return ...  

    #body : actually what function/program does.   

    # returning , finishing statement.

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