builtins.Unboundlocalerror: local variable

Asked

Viewed 42 times

-1

I’m making a program to find the biggest cousin but I’m making the following mistake: builtins.UnboundLocalError: local variable 'Entrada' referenced before assignment What could it be? The variable Entrada was defined, I don’t understand....

def Maior_Primo(X):
    X = N = Entrada
    Primo = 0
    RestoPrimo = 0

    while Entrada > 0 : 
        if Entrada == 0:
            MP ( 0 )

        else:
            while N > 0 :
                Divisão = Entrada % N  
                if Divisão == 0:
                    RestoPrimo = RestoPrimo + 1
                N = N - 1

        if RestoPrimo == 2 :
            if Entrada > Primo:
                Primo = Entrada
                Entrada = Entrada - 1
        return Primo

print(Maior_Primo(2))
  • In the first line of the function you have defined that X will be equal to N that will be equal to Input. What is the Input value at this point of the code? The variable does not yet exist within the function; by chance it created it outside?

1 answer

0

Well, it worked now, it’s like this.

coding: utf-8

def maior_primo(x): Input = x N = x Primo = 0 Restoprimo = 0

while Entrada > 0 : 
    if Entrada == 0:
        MP ( 0 )

    else:
        while N > 0 :
            Divisão = Entrada % N  
            if Divisão == 0:
                RestoPrimo = RestoPrimo + 1
                N = N - 1
            else:
                N = N - 1

        if RestoPrimo == 2 :
            if Entrada > Primo:
                Primo = Entrada
                Entrada = Entrada - 1
                N = Entrada
                RestoPrimo = 0
            else:
                Entrada = Entrada - 1
                N = Entrada
                RestoPrimo = 0

        else:
            Entrada = Entrada - 1
            N = Entrada
            RestoPrimo = 0
return( Primo )
  • What has changed? What was wrong? Why was it wrong?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.