[Tutor] Help with Code (beginner) Error message

Gonçalo Rodrigues op73418@mail.telepac.pt
Tue Nov 19 20:04:01 2002


----- Original Message -----
From: "DAVID BRALLIER" <bralzd@hotmail.com>
To: <Tutor@python.org>
Sent: Wednesday, November 20, 2002 12:47 AM
Subject: [Tutor] Help with Code (beginner) Error message


> Trying to make a simple program that manages a small bank account.
(College
> assignment) I get an error message saying that "deposit is not defined"
but
> as you can see I have defined it. Here is the code:
>
>
>
> #Account manager program
> from time import time, ctime
> class account:

A point of style: class names are usually capitalized, e.g. Account.

>     def __init__(self,initial):
>         self.balance = initial
>     def withdraw(self,amt):
>         return initial - amt

What is initial here? it is not defined anywhere. Shouldn't it be
self.balance? Besides, if you are *withdrawing* money it should be

self.balance = self.balance - amount

Why the return statement?

>     def deposit(self, amt):
>         self.balance = self.balance + amt
>     def getbalance(self):
>         return self.balance
>
>
> list = [deposit(550.23),deposit(100), withdraw(50)]

You are calling deposit as if it were a global function. While above you
declared deposit as method of the class account - two very different things.
That is you get the error you get - there is no global deposit function.
What you want is probably instantiate the account class as in

a = account(0)

and *then* call a.deposit(550.23), etc.

You should prolly go through the tutorial that comes with every Python
distro and lookup the topic on classes to review the subject. Or go to the
newbies page in www.python.org to find links to other tutorials.

> for transaction in list:
>     print "The balance is: ", transaction.getbalance()
>     print "The time of your transaction is: ", ctime (time())
>
>

HTH,
G. Rodrigues