I found the bug. In line 6, I had print " " instead of return " ".
So it runs now, but there is a different problem
it doesn’t save the balance when i use print my_account. It is always the same statement.
but when I use show_balance () it shows the right balance.
And sometimes it prints the 2 decimals and sometimes not.
This account belongs to Mike and has a balance of $ 0.00
Your total balance is $0.00
You are depositing $ 2000.00
Your total balance is $2000.00
You are withdrawing $1000.00
Your total balance is $1000.00
This account belongs to Mike and has a balance of $ 1000.00
with this code:
class BankAccount(object):
balance = 0
def __init__(self, name):
self.name = name
def __repr__(self):
return "This account belongs to %s and has a balance of $ %.2f" % (self.name, self.balance)
def show_balance(self):
print "Your total balance is $%.2f" % self.balance
def deposit(self, amount):
if amount <= 0 :
print "Invalid deposit"
return
else :
print "You are depositing $ %.2f" % amount
self.balance += amount
self.show_balance()
def withdraw(self, amount):
if amount > self.balance :
print "Error, you can't withdraw more than you have dummy!"
return
else :
print "You are withdrawing $%.2f" % amount
self.balance -= amount
self.show_balance()
my_account = BankAccount("Mike")
print my_account
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)
print my_account
i only changed print to return, and everything seems to work fine?