+ 1
Trying and failing to use class attributes
Hey, Iām trying to do a practice problem where I defined a class BankAccount and a function that takes user input and adds an integer value to the balance attribute, but the addition isnāt working. Can anyone help? https://code.sololearn.com/co0s2hdtPApt/?ref=app
2 Answers
+ 1
class BankAccount:
def __init__(self, balance):
self._balance = balance
def __repr__(self):
return "Account Balance: {}".format(self._balance)
def deposit(self, amount):
self._balance += amount
return self._balance #you are not saving deposit here
acc = BankAccount(0)
acc.deposit(int(input()))
acc.deposit(int(input()))
print(acc)
+ 1
I tried this before, but I didnt seperate the self._balance += amount and return statements (e.g. I did return self._balance += amount). I see now why that doesnāt work. This answera the question, thank you!