[Tutor] class question
christopher.henk at gm.com
christopher.henk at gm.com
Fri Jul 13 21:35:37 CEST 2007
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: christopher.henk at gm.com
Dick Moores <rdm at rcblue.com>
Sent by: tutor-bounces at python.org
07/13/2007 03:04 PM
To
Tiger12506 <keridee at jayco.net>, <tutor at 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 at python.org
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070713/9f5b5455/attachment.html
More information about the Tutor
mailing list