Estoy intentando actualizar el valor de una variable de clase cuando alguien deposita dinero en mi banco. Mi clase de código Bank_account (): bank_balance = 0 def __init __ (self, name_holder, id, balance): ...

-1
Halley 15 ene. 2021 a las 11:43

4 respuestas

La mejor respuesta

Si es absolutamente necesario, intente lo siguiente:

(Observe que agregué: Bank_account.bank_balance porque eso también inicializa la variable de clase. Verifique que la salida producida sea la que desea)

class Bank_account:
    
    bank_balance = 0
    
    def __init__(self, name_holder, id, balance):
        self.name = name_holder
        self.id = id
        self.balance = balance
        Bank_account.bank_balance += balance   # change is here
        
    def cash_in(self, cash):
        self.balance += cash
        bank_balance += cash
        
    def with_draw(self, draw):
        if self.balance < draw or bank_balance < draw:
            print('Error')
        else:
            print('Here your money!')


John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)

print(Bank_account.bank_balance)
0
Dharman 15 ene. 2021 a las 08:58
class Bank_account():
    def __init__(self, name_holder, id, balance,bank_balance=0):
        self.name = name_holder
        self.id = id
        self.balance = balance
        self.bank_balance = balance
        
    def cash_in(self, cash):
        self.balance += cash
        self.bank_balance += cash
        
    def with_draw(self, draw):
        if self.balance < draw or bank_balance < draw:
            print('Error')
        else:
            print('Here your money!')

John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)

print(John.bank_balance)

Creo que esto es lo que estás tratando de hacer.

1
Goldwave 15 ene. 2021 a las 08:47

Clase Bank_account ():

bank_balance = 0

def __init__(self, name_holder, id, balance):
    self.name = name_holder
    self.id = id
    self.balance = balance
    self.bank_balance += balance
    
def cash_in(self, cash):
    self.balance += cash
    bank_balance += cash
    
def with_draw(self, draw):
    if self.balance < draw or bank_balance < draw:
        print('Error')
    else:
        print('Here your money!')

John = Cuenta_bancaria ('John', 123456, 2.1 - ** 6)

Smith = Cuenta_bancaria ('Smith', 111111, 3.10 ** 6)

Michael = Cuenta_bancaria ('Michael', 123123, 4.10 ** 6)

Imprimir (John.bank_balance)

0
Noor Ahmed Natali 15 ene. 2021 a las 08:52

Dado que el código dentro de una clase, pero fuera de los métodos / funciones no se ejecuta, la variable no se declara en la memoria. Entonces, la solución sería declararlo en el método init de la siguiente manera:

class Bank_account():


    def __init__(self, name_holder, id, balance):
        # This has been changed 
        bank_balance = 0
        self.name = name_holder
        self.id = id
        self.balance = balance
        bank_balance += balance
        
    def cash_in(self, cash):
        self.balance += cash
        bank_balance += cash
        
    def with_draw(self, draw):
        if self.balance < draw or bank_balance < draw:
            print('Error')
        else:
            print('Here your money!')

John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)

Bank_account.bank_balance

O si desea acceder al saldo bancario separado de cada objeto bank_account, simplemente agregue una línea que diga global bank_balance de la siguiente manera:

class Bank_account():


    def __init__(self, name_holder, id, balance):
        # This has been changed
        global bank_balance
        bank_balance = 0
        self.name = name_holder
        self.id = id
        self.balance = balance
        bank_balance += balance
        
    def cash_in(self, cash):
        self.balance += cash
        bank_balance += cash
        
    def with_draw(self, draw):
        if self.balance < draw or bank_balance < draw:
            print('Error')
        else:
            print('Here your money!')

John = Bank_account('John', 123456, 2.10**6)
Smith = Bank_account('Smith', 111111, 3.10**6)
Michael = Bank_account('Michael', 123123, 4.10**6)

Bank_account.bank_balance

Pero recomiendo encarecidamente agregar self antes de la variable bank_balance

0
SnazzyMohit 15 ene. 2021 a las 09:02
65732940