Nameerror: name 'setSaldo' is not defined

Asked

Viewed 1,327 times

0

I am learning POO in python and am having a problem. When I try to use the getters and setters I am not getting.

#Método Construtor
def __init__(self):
    self.saldo = 0
    self.status = False

#Métodos Especiais
def getSaldo(self):
    return self.saldo
def setDono(self, d):
    self.dono = d

    #Demais Métodos
def abrirConta(self):
    if self.status == False:
        a = input('Quem é o titular da conta? ')
        setDono(a)
        self.tipo = input('Qual é o tipo da conta? ')
        self.status = True
        if self.tipo == 'CC':
            self.saldo = 50
            print('Parabéns, sua conta corrente está aberta. Você já tem 50 reais de saldo.')
        elif self.tipo == 'CP':
            self.saldo = 150
            print('Parabéns, sua conta poupança está aberta. Você já tem 150 reais de saldo.')

When I call Setter setDono in the same document of the class it gives me error, saying that setDono is not defined. If I call it in another document, as in an instantiated object it works, but the getters return what seems to me to be the location in memory that the data is allocated, rather than what it contains

  • 1

    This error happens because your class does not have this owner attribute set. Try placing the constructor equally to the attribute balance and status.

  • I did that, I put it in the self.dono builder and it keeps repeating the error. Some other idea?

  • What was the line that made the mistake? Post it and post the def setSaldo also. Check if you have any "decorator" in setSaldo.

1 answer

2


To call the method/function setDono() you need to specify that you are calling the class method itself, thus doing self.setDono(). Also, I recommend that when creating attributes in the constructor, insert a underscore at the beginning of the attribute name indicating that it is a private class attribute (it is an adopted convention) just as I did.

class Conta:

    def __init__(self):
        self._saldo = 0
        self._status = False
        self._dono = ""

    #Métodos Especiais
    def getSaldo(self):
        return self._saldo
    def setDono(self, d):
        self._dono = d

        #Demais Métodos
    def abrirConta(self):
        if self._status == False:
            a = input('Quem é o titular da conta? ')
            self.setDono(a)
            self.tipo = input('Qual é o tipo da conta? ')
            self._status = True
            if self.tipo == 'CC':
                self._saldo = 50
                print('Parabéns, sua conta corrente está aberta. Você já tem 50 reais de saldo.')
            elif self.tipo == 'CP':
                self._saldo = 150
                print('Parabéns, sua conta poupança está aberta. Você já tem 150 reais de saldo.')
  • Yes, I didn’t put all the code in the post to not get too big, but I did indicate that it’s a class.

  • Next, the problem of setDono() not being shown is that to call you must do self.setDono() as I put it in my reply.

  • Thank you very much!! This I was not understanding!

  • 1

    @Rust, If everything went right and your problem has been solved, please mark the answer as correct so that others who have the same problem you see and can solve the same problem

Browser other questions tagged

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