Trying and failing to use class attributes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Feb 2022, 7:17 PM
Azazel
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)
3rd Feb 2022, 7:25 PM
Jayakrishna 🇮🇳
+ 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!
3rd Feb 2022, 7:29 PM
Azazel