The output from my code is not in the correct order. It should first tell me I have a zero balance, then go on to deposit, then withdraw, then show a final balance of 1000.00. Instead, it looks like this...
You are withdrawing: 1000.00
Your balance is now: 1000.00
Account Holder: Justin has a balance of: $1000.00
Account Holder: Justin has a balance of: $0.00
Your balance is now: 0.00
You are depositing: 2000.00
Your balance is now: 2000.00
I'm also still confused about having balance = 0 outside of the init method. What is the benefit of having it outside of the init block? I thought it's related to scope, but I still have to put 'self.' in front of it throughout the class definition.
class BankAccount(object): balance = 0 def __init__(self, name): self.name = name def __repr__(self): return 'Account Holder: %s has a balance of: $%.2f' \ % (self.name, self.balance) def show_balance(self): print 'Your balance is now: %.2f' % (self.balance) def deposit(self, amount): if amount <= 0: print 'ERROR: Amount deposited must be \ greater than zero.' else: print 'You are depositing: %.2f' % (amount) self.balance += amount self.show_balance() def withdraw(self, amount): if amount > self.balance: print 'Insufficient funds.' else: print 'You are withdrawing: %.2f' % (amount) self.balance -= amount self.show_balance() my_account = BankAccount('Justin') print my_account my_account.show_balance() my_account.deposit(2000.00) my_account.withdraw(1000.00) print my_account