Why does this not work?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this not work??

Given the code, I am trying to get a result which adds the balance to the deposit amount. Where am I going wrong? class BankAccount: def __init__(self, balance): self._balance = balance def __repr__(self): return "Account Balance: {}".format(self._balance) def __deposit__(self, deposit): self._deposit = deposit return self._balance + deposit acc = BankAccount(0) acc._deposit(int(input())) print(acc)

4th Sep 2020, 1:14 AM
Dylan Woody
Dylan Woody - avatar
3 Answers
+ 3
__deposit__ is a magic method but it's not built-in, you can't create your own magic methods. Try just deposit without underscores
4th Sep 2020, 1:18 AM
Bagon
Bagon - avatar
+ 1
Solution: the final print call is calling the class BankAccount() so I need to change the last line of the deposit method to reassign the new account balance to self. _balance = self. _balance + self. _deposit
4th Sep 2020, 1:33 AM
Dylan Woody
Dylan Woody - avatar
0
Bagon is the syntax right to get the method to return the sum of balance and deposit when called upon? I'm getting Account Balance: 0 instead of whatever the input is
4th Sep 2020, 1:29 AM
Dylan Woody
Dylan Woody - avatar