I am learning python and while going through this OOP'S exercise:
For this challenge, create a bank account class that has two attributes: owner balance and two methods: deposit withdraw As an added requirement, withdrawals may not exceed the available balance.
Now the problem that I am facing is when I run the withdrawal once it works fine, but when I work it the second time it shows the error
" TypeError Traceback (most recent call last) /var/folders/15/yqw5v0lx20q5lrbvg8bb69jr0000gn/T/ipykernel_79159/1232198771.py in ----> 1 acct1.withdraw(200)
TypeError: 'int' object is not callable"
here is my code
class Account:
def __init__(self, owner, balance = 0):
self.owner = owner
self.balance = balance
def __str__(self):
return f"the account holder is {self.owner} \nand the balance is {self.balance}"
def deposit(self,deposit):
self.deposit = deposit
self.balance = deposit
print("deposit accepted")
def withdraw(self, withdraw):
self.withdraw = withdraw
if self.balance >= withdraw:
self.balance -= withdraw
print("money withdrawn")
else:
print("Funds Unavailable!")
Kindly let me know where am I going wrong.
CodePudding user response:
Do not name a method the same as a class attribute:
def withdraw(self, withdraw):
self.withdraw = withdraw
Possible solution:
def perform_withdraw(self, withdraw):
self.withdraw = withdraw
CodePudding user response:
Before the first call to withdraw, the attribute withdraw of an instance of Account is the method that does your calculation.
After the first call to withdraw, the attribute is whatever argument you called withdraw with, because you issue self.withdraw = withdraw. Use another name or remove the line altogether if it is not needed.
