1

So I have a problem with this code:

class Cliente:
    def __init__(self,nome,telefone):
        self.nome = nome;
        self.telefone = telefone;

class Conta:
    def __init__(self,clientes,numero,saldo=0):
        self.saldo = saldo
        self.numero = numero
        self.clientes = clientes
    def resumo(self):
        print('Cliente: {0:s} CC Numero: {1:s} Saldo: {2:10.2f}'.format(self.clientes.nome, self.numero, self.saldo))
    def saque(self,valor):
        if self.saldo >= valor:
            self.saldo -= valor
    def deposito(self,valor):
        self.saldo += valor

When I go to test my class:

from tatu import Cliente
from tatu import Conta

joao = Cliente('Joao da Silva','666-6666')
maria = Cliente('Maria da Penha','234-5678')
conta1 = Conta(joao,'0138',1500)
conta1.deposito(800)
conta1.saque(1300)
conta2 = Conta([joao,maria],'0139',1000)
conta2.deposito(1000)
conta1.resumo()
conta2.resumo()

My second account don't print and I have this error:

print('Cliente: {0:s} CC Numero: {1:s} Saldo: {2:10.2f}'.format(self.clientes.nome, self.numero, self.saldo))
AttributeError: 'list' object has no attribute 'nome'
3
  • 2
    Your second account is a list of Cliente objects, not a single object. Commented Oct 14, 2014 at 16:29
  • Yes, I want an account with two clients, how to declare it ? Commented Oct 14, 2014 at 16:30
  • @JhonatanMark Do you want an account with two clients, or an account with multiple clients? If it's just two then you can have self.client1 = client1; self.client2 = client2. If you want multiple then you should be passing in a list. If you want to get all names for every client in a list then you can use list comprehensions or map: [client.nome for client in self.clientes] or map(lambda client: client.nome, self.clientes). Commented Oct 14, 2014 at 16:33

2 Answers 2

2

You are mixing types; sometimes your clientes is one Cliente instance, sometimes it is a list.

Always make it a list instead, and then loop over that list:

# account with just one client
conta1 = Conta([joao],'0138',1500)
# account with two clients
conta2 = Conta([joao,maria],'0139',1000)

and now that it is always a list, use a loop:

def resumo(self):
    for client in self.clientes:
        print('Cliente: {0:s} CC Numero: {1:s} Saldo: {2:10.2f}'.format(client.nome, self.numero, self.saldo))

or perhaps split out the client names list and print account number and saldo separately:

def resumo(self):
    for client in self.clientes:
        print('Cliente: {0:s}'.format(client.nome))
    print('CC Numero: {1:s} Saldo: {2:10.2f}'.format(self.numero, self.saldo))
Sign up to request clarification or add additional context in comments.

Comments

0

self.clientes is not an instance of Cliente, it is a list of instances. The list itself does not have the nome property, only the instances do. You would need to iterate through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.