[Tutor] Help with Code (beginner) Error message

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed Nov 20 01:31:01 2002


> 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"

Hi David,

We cannot give direct help on your homework assignment.  We'll do what we
can to point you in the general direction, but it's your homework.


> but as you can see I have defined it.

If you've gotten this far in a college programming class, you have to
realize how relentlessly faithful a computer is.  I can't think of a
subtle way to say this: you have to be humbler about your computer
programs.

All too often, the computer is far too perfect a servant: it obeys the
schemes and machinations of imperfect beings --- it follows only what we
type, and not what we mean.



> class account:
>     def __init__(self,initial):
>         self.balance = initial
>     def withdraw(self,amt):
>         return initial - amt
>     def deposit(self, amt):
>         self.balance = self.balance + amt
>     def getbalance(self):
>         return self.balance


Have you tested your account class?  Have you tried out all the methods in
your class?  Which parts still behave buggily?  More importantly, which
parts appear to be working well, and why?


Just as functions can be tested by calling them and by looking at their
return values, classes too can be tested by instantiating them and trying
out their methods.  You really need to try out your code, especially since
this is all very new stuff.


There are a few references and tutorials on the web that you can use as
examples of defining classes.  Many of them are linked from the Newcomer's
page on python.org:

    http://python.org/doc/Newbies.html


Here are two pages in particular that may help you:

    http://www.ibiblio.org/obp/thinkCSpy/chap12.htm
    http://www.freenetpages.co.uk/hp/alan.gauld/

They pages show ways of instantiating and calling methods, so you may want
to try those examples out first.  You may find that going through these
tutorials will help you with your original problem.


Good luck to you.