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" Chris Henk Allison Transmission phone: 317.242.2569 fax: 317.242.3469 e-mail: [EMAIL PROTECTED] Dick Moores <[EMAIL PROTECTED]> Sent by: [EMAIL PROTECTED] 07/13/2007 03:04 PM To Tiger12506 <[EMAIL PROTECTED]>, <tutor@python.org> cc Subject Re: [Tutor] class question At 05:35 AM 7/13/2007, Tiger12506 wrote: > > ======================================= > > 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 > > def overdrawn(self): > > return self.balance < 0 > > my_account = BankAccount(15) > > my_account.withdraw(5) > > print my_account.balance > > ========================================= > > > > This prints the expected "10". > > > > My question is, of what use can "overdrawn" be put? If I change the > > withdrawal amount to 25, it prints the expected "-10", whether the class > > contains the "overdrawn" function or not. > > > > Thanks, > > > > Dick Moores > >A very good question. Now I have one for you. What does your bank do when >you try to withdraw money? First, it checks to see if you have the money in >your account. If you do, it subtracts that out of your balance. Whoever >wrote that code failed to do the check within the withdraw function. > >======================================= >class BankAccount(object): > def __init__(self, initial_balance=0): > self.balance = initial_balance > def deposit(self, amount): > self.balance += amount > def withdraw(self, amount): > if self.overdrawn(): > raise "Insufficient Funds Error" > self.balance -= amount > def overdrawn(self): > return self.balance < 0 >my_account = BankAccount(15) >my_account.withdraw(5) >print my_account.balance >========================================= But when I run that with a withdrawal of 25, it still prints only "-10". How have you involved the "overdrawn" function? Dick _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor