My Python class is working but has the result values not match

Asked

Viewed 60 times

0

I’m creating a Chain Account class. In this class I want to have the balance of each account and the totaling of all accounts, but is giving error in the balance of the second account, the class is taking the balance of the first account.

Function that prints the result:

def imprimeSaldos():
    print('-' * 30)
    print(f'Itau Credito: {itau.conta} ->> {itau.totalCredito}')
    print(f'Itau Debito.: {itau.conta} ->> {itau.totalDebito}')
    print(f'Itau Saldo..: {itau.conta} ->> {itau.saldo}')

    print('-' * 30)
    print(f'Caixa Credito: {caixa.conta} ->> {caixa.totalCredito}')
    print(f'Caixa Debito.: {caixa.conta} ->> {caixa.totalDebito}')
    print(f'Caixa Saldo..: {caixa.conta} ->> {caixa.saldo}')

    print('-' * 30)
    print(f'Total de Credito: {Conta.totalCredito}')
    print(f'Total de Credito: {Conta.totalDebito}')
    print(f'Saldo...........: {Conta.saldo}')

My class:

class Conta(object):
    totalCredito = 0
    totalDebito = 0
    conta = ''
    saldo = 0

    def __init__(self, pConta = 0, pSaldo = 0):
        # Alimenta os valores da cota
        self.conta = pConta
        self.totalCredito += pSaldo
        self.saldo = pSaldo

        # Alimenta os valores totais da classe
        Conta.totalCredito += pSaldo
        Conta.saldo = Conta.totalCredito - Conta.totalDebito

    def Deposito(self, pValor = 0):
        # Alimenta os valores da cota
        self.totalCredito += pValor

        # Alimenta os valores totais da classe
        Conta.totalCredito += pValor
        Conta.saldo = Conta.totalCredito - Conta.totalDebito

    def Saque(self, pValor = 0):
        # Alimenta os valores da cota
        self.totalDebito += pValor

        # Alimenta os valores totais da classe
        Conta.totalDebito += pValor
        Conta.saldo = Conta.totalCredito - Conta.totalDebito

Instances of my accounts:

itau = Conta('123-4', 50)
caixa = Conta('321-0', 0)

Outturn:

------------------------------
Itau Credito: 123-4 ->> 50
Itau Debito.: 123-4 ->> 0
Itau Saldo..: 123-4 ->> 50
------------------------------
Caixa Credito: 321-0 ->> 50
Caixa Debito.: 321-0 ->> 0
Caixa Saldo..: 321-0 ->> 0
------------------------------
Total de Credito: 50
Total de Credito: 0
Saldo...........: 50

Note that even though I passed 0 (zero) to the cashier account, She got 50 Credit Kings.

This is wrong is not even?

  • Solutions to the problem should not be in the question. The answer should be posted as the answer. Your question has been closed as duplicate because it already has answers on other topics. If you disagree with the closing, please [Dit] ask the question and clarify what is the difference of your problem to others. Taking advantage, what you did as a "solution" is not a solution to the problem. I recommend that you read the above questions and understand what the problem is, and research what class and instance attributes are in Python.

  • I do not know where is the option to answer this question. I would have liked to have responded by posting the solution I found. But I do not know how to do.

  • You can’t answer because the question was closed. If you think she answers your problem, she should also answer the questions cited, so you could answer for them and not here. You even searched for instance and class attributes?

  • Yes. I did some research. I realized what I was doing was wrong. Then I discovered that the attributes of the instance had nothing to do with the attributes of the class.

No answers

Browser other questions tagged

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