Why does the list error Object has in the 'checks' attribute occur?

Asked

Viewed 46 times

-1

I’m building a program based on a class diagram, and it appears

Attributeerror: 'list' Object has no attribute 'checks' when I print the list with the client’s checks

class Cheque:
    def __init__(self, **D):
       self.numConta = D.pop('nc', '------ -')
       self.numCheque = D.pop('nch', '?')
       self.valor = D.pop('v', '0.00')
       self.dta_emissao = D.pop('em', 'dd/mm/aaaa')
       self.dta_vcto = D.pop('vcto', 'dd/mm/aaaa')
       self.dta_deposito = D.pop('dep', 'dd/mm/aaaa')
       self.obs_verso = D.pop('obs', '?')

class Cliente:
    def __init__(self,*listaCheque,**D):
        self.codigo   = D.pop('cod','--')
        self.nome     = D.pop('n','--')
        self.fone     = D.pop('fone','(--) ----- ----')
        self.data     = D.pop('dt','--/--/----')
        self.Endereco = D.pop('end','--')
        self.Cidade   = D.pop('cid','--')
        self.UF       = D.pop('uf','--')
        self.cheques  = []
        for x in listaCheque:
            self.cheques.append(x)
        self.tipo     = D.pop('tipo', '--')
    def listaCheque(self):
        return self.cheques
    def adicionaCheque(self, novoCheque):
        if novoCheque not in self.cheques:
            self.cheques.append(novoCheque)
    def excluiCheque(self, chequeAtual):
        if chequeAtual in self.cheques:
            self.cheques.remove(chequeAtual)

cheque1 = Cheque(nc = '123516-0', nch ='aaaa', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque2 = Cheque(nc = '123516-0', nch ='bbbb', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque3 = Cheque(nc = '123516-0', nch ='cccc', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque4 = Cheque(nc = '193516-0', nch ='dddd', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque5 = Cheque(nc = '163516-0', nch ='eeee', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque6 = Cheque(nc = '183516-0', nch ='ffff', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')

clientef1 = Cliente([cheque1,cheque3],cod = 44, n= 'Carlos', fone ='(41)9 93356-8903',\
                   dt ='08/03/2016', end = 'Rua X,123', cid ='Curitiba',\
                       uf = 'Pr', tipo = 'PF')

novoCheque = clientef1.adicionaCheque(cheque4)
for c in clientef1.listaCheque():    
    print(c.cheques)
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

You are receiving a list of checks and with the for takes each of them, so there’s nothing called cheques within each cheque, has for example, valor.

But there is another problem that will make it not work. When passing as argument a list has to take only one argument, so you cannot use the asterisk that is to take several arguments as if it were a list (formally a tuple).

Or should not pass a list and yes several arguments, but I would not do this, actually I would not even pass the checks when creating the account, this is confusing and makes the builder have more of a responsibility.

You really should make a constructor that gets the argument you want explicitly, the way you did it using kwargs is to ask to make mistakes when coding. Especially in a constructor this should not be accepted.

I would change other things, but for now you already have some tips.

class Cheque:
    def __init__(self, **D):
       self.numConta = D.pop('nc', '------ -')
       self.numCheque = D.pop('nch', '?')
       self.valor = D.pop('v', '0.00')
       self.dta_emissao = D.pop('em', 'dd/mm/aaaa')
       self.dta_vcto = D.pop('vcto', 'dd/mm/aaaa')
       self.dta_deposito = D.pop('dep', 'dd/mm/aaaa')
       self.obs_verso = D.pop('obs', '?')

class Cliente:
    def __init__(self, listaCheque, **D):
        self.codigo   = D.pop('cod','--')
        self.nome     = D.pop('n','--')
        self.fone     = D.pop('fone','(--) ----- ----')
        self.data     = D.pop('dt','--/--/----')
        self.Endereco = D.pop('end','--')
        self.Cidade   = D.pop('cid','--')
        self.UF       = D.pop('uf','--')
        self.cheques  = []
        for x in listaCheque:
            self.cheques.append(x)
        self.tipo     = D.pop('tipo', '--')
    def listaCheque(self):
        return self.cheques
    def adicionaCheque(self, novoCheque):
        if novoCheque not in self.cheques:
            self.cheques.append(novoCheque)
    def excluiCheque(self, chequeAtual):
        if chequeAtual in self.cheques:
            self.cheques.remove(chequeAtual)

cheque1 = Cheque(nc = '123516-0', nch ='aaaa', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque2 = Cheque(nc = '123516-0', nch ='bbbb', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque3 = Cheque(nc = '123516-0', nch ='cccc', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque4 = Cheque(nc = '193516-0', nch ='dddd', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque5 = Cheque(nc = '163516-0', nch ='eeee', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')
cheque6 = Cheque(nc = '183516-0', nch ='ffff', v ='1' , em = '2', vcto ='3' , dep ='4' , obs ='5')

clientef1 = Cliente([cheque1,cheque3],cod = 44, n= 'Carlos', fone ='(41)9 93356-8903',\
                   dt ='08/03/2016', end = 'Rua X,123', cid ='Curitiba',\
                       uf = 'Pr', tipo = 'PF')

novoCheque = clientef1.adicionaCheque(cheque4)
for c in clientef1.listaCheque():    
    print(c.valor)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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