However if you try to withdraw any money after you took out the $25 it would raise the error.
The overdrawn function checks if you are in the negatives.
Since the balance is checked before the money is taken out, there is no error when you take out the $25.
If you wan the error to be raised whenever you go negative simply switch the withdrawal and the function call in withdraw
def withdraw(self, amount):
self.balance -= amount
if self.overdrawn():
raise "Insufficient Funds Error"
and if you don't want the money taken out if there is insufficient funds,
just add the withdrawn amount back:
def withdraw(self, amount):
self.balance -= amount
if self.overdrawn():
self.balance += amount
raise "Insufficient Funds Error, no money withdrawn"
Thanks. You've clarified it for me completely.
Your second way seems to make more sense. And instead of raising the error, why not just print it:
class BankAccount(object):
def __init__(self, initial_balance=0):
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
if self.overdrawn():
self.balance += amount
print "Insufficient Funds, no money withdrawn"
def overdrawn(self):
return self.balance < 0
my_account = BankAccount(15)
my_account.withdraw(25)
print "Account balance is", my_account.balance
Dick
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor