TypeError

Jeff Shannon jeff at ccvcorp.com
Tue Aug 28 18:14:00 EDT 2001


Martijn wrote:

> It still doesn`t work. This is a code snippet example from Python 2.1 Bible:
>
> class Wallet:
>         "where does my money go ?"
>                 WalletCnt = 0
>         def __init__(self,balance = 0):
>                 self.balance = balance
>                 Wallet.WalletCnt = Wallet.WalletCnt + 1
>         def getPaid(self,amnt):
>                 self.balance = self.balance +1
>                 self.display()
>
>         def spend(self,amnt):
>                 self.balance = self.balance -1
>                 self.display()
>
>         def display:
>                 print 'New Balance: $%.2f; % self.balance

Hm, I suspect there may be a couple of errors--the getPaid() and spend() methods
probably want to be adjusting self.balance by amnt, rather than 1;  also, the
display() method is missing a ' to close the string.  I'm going to presume those
are changed in the following examples.

Okay, to use this class from another file...  (presuming that this above class
is in a file called wallet.py)

>>> import wallet
>>> mywallet = wallet.Wallet(100.00)
>>> mywallet.getPaid(50.00)
New Balance: $150.00
>>> mywallet.spend(23.99)
New Balance: $126.01
>>> mywallet.getPaid(600.00) # payday!
New Balance: $726.01
>>> rent = 800.00
>>> mywallet.spend(rent) # uh oh!
New Balance: $-73.99
>>> mywallet.WalletCnt
1
>>> wallet.Wallet.WalletCnt   # this is the same thing!
1

Does this help?

Jeff Shannon
Technician/Programmer
Credit International






More information about the Python-list mailing list