If I run the program, t although the beginning part of the program works quite well, the last line of the program shows the balance 0 and it is different from the results that we can get from the second last line.
I am getting very confused with why it works like this. Could anyone explain why the program works like this and tell me how to fix the bug please?
Thank you in advance!
"""The program is to virtually create a bankaccount""" class BankAccount(object): balance = 0 def __init__(self, name): self.name = name def __repr__(self): return "%s's account. Balance: $%.2f" % (self.name, self.balance) def show_balance(self): print "%s's account. Balance: $%.2f" % (self.name, self.balance) def deposit(self, amount): if amount <= 0: return else: print "Depositing..." print float(amount) self.balance += amount self.show_balance() def withdraw(self, amount): if int(amount) > int(self.balance): print "The amount you can withdraw should be less than or equal to your balance!" return else: print "Withdraw amount: %s" % (amount) self.balance -= amount self.show_balance() my_account = BankAccount("Sean") print my_account print my_account.show_balance() a = BankAccount("Sean") a.deposit(2000) a.withdraw(500) print my_account