0

I have the following code but Python keeps saying "local variable 'a' referenced before assignment"

def inter(X,Y):
    if pertenece(cabeza(X),Y):
        a = lista(cabeza(X),inter(cola(X),Y))
    elif vacia(cola(X)): return a
    else: inter(cola(X),Y)

I have no idea why I'm getting this error since I have defined 'a' before referencing it. Please help.

The functions 'pertenece', 'cabeza', 'cola', 'vacia' and 'lista' where all previously defined and have no issue.

2 Answers2

1

I think this is because you're initializing var a in an if clause, and refrencing it in another if clause, so considering the condition it may or may not be initialized befored it is referenced in if clause. Can you try to initialize a within the function body before your first if clause, to a empty or null value. Maybe that could help.

0

You have only defined a in the first conditional statement. If the code never goes through that condition, then a will never be defined. You would have to assign some default value for a before the conditional statements in order to prevent the return statement from throwing an error at you.

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37