Why doesn’t the program recognize the "N"?

Asked

Viewed 64 times

-4

class primao:
    from random import randrange

    def __init__(self, N=randrange(2,100), divisor=0, pontos=0, laco=True, prova_cabal= N%divisor):
        self.N= N
        self.divisor= divisor
        self.pontos= pontos
        self.laco= laco
        self.prova_cabal= prova_cabal

    def gerar_primo(self):
        for i in range(1,self.N,+1):
            self.divisor += 1                       
            if self.prova_cabal == 0:
                self.pontos += 1
            if self.pontos > 2:
                self.laco= False
        if laco == False:
            gerar_primo()   
        if pontos <= 2:
            self.laco = True
            return self.N


A= primao()
A.N_Primo()

1 answer

2


The error is being generated because you did not initialize the variables N and divisor in its class or out before being used to define the parameter prova_cabal = N % divisor.

I don’t know what your goal is with this code, but if you initialize these variables, the error will be deleted. Example:

class Primao(object):
    from random import randrange

    N = 1
    divisor = 1

    def __init__(self, N = randrange(2,100), divisor=0, pontos=0, laco=True, prova_cabal = N % divisor):

        self.N = N
        self.divisor = divisor
        self.pontos = pontos
        self.laco = laco
        self.prova_cabal = prova_cabal

    # Código restante...

Other than that your code is full of error. You are calling the method N_Primo() that doesn’t exist, you forgot to use self when calling the attribute laco and the method gerar_primo, and even with these corrected details, your code still generates the following error:

Recursionerror: Maximum recursion Depth exceeded in comparison

This error is being generated because you exceed the maximum allowed calls of the same method in Python.

I suggest you review your code right, looking for typos to fix them and improve the logic of your code.

Browser other questions tagged

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