Re: writing a class

From: Alexander Hoffmann (alexander.hoffmann_at_netgenius.de)
Date: 09/08/04


To: python-list@python.org
Date: Wed, 8 Sep 2004 21:01:49 +0200

On Wednesday 08 September 2004 20:15, Crypt Keeper wrote:
> It's a simple bank-type transaction program. User needs to input
> initial starting balance, amount of deposits, amount of withdrawals.
> Program needs to do the appropriate math and return status update
> showing new balance, total deposits and total withdrawals.
>
> I keep coming up with 2 different results...1 returns an error message
> and the other finishes the program but the mathmematics is wrong (it
> does not compute deposits and withdrawlas. It only returns intial
> balance as new balance.)
>
> Code for error message return is:
>
> (formal code)
> from Account import Account
> from datetime import date
>
> a = Account()
> now = date.today()
>
> print "As of",now,"balance is $",a.getbalance()
>
> (class code)
> class Account:
>
> def __init__(self, initial):
> self.balance = initial
> def deposit(self, amt):
> self.balance = balance + amt
> def withdraw(new, amt):
> self.balance = balance - amt
> def getbalance(self):
> return self.balance
>
> Error message that gets returned is:
>
> Traceback (most recent call last):
> File "C:\Python23\Module1a.py", line 4, in -toplevel-
> a = Account()
> TypeError: __init__() takes exactly 2 arguments (1 given)
This one is simple: the constructor of your class Account takes two arguments
("def __init__(self, initial):"). In object oriented Python programming the
first parameter is always implicitly given to a method (it's a reference to
the actual instance called "self"). That means by typing "a = Account ()" you
do indirectly call "Account.__init__ (self)". Here the second param "initial"
is missing, e.g.: a = Account (0)

>
>
> The code that returns new balance only is:
>
> (class code)
> class Account:
>
> def __init__(self, balance):
> self.balance = balance
> def deposit(self, deposit):
> self.balance = self.balance + deposit
> def withdraw(self, withdraw):
> self.balance = self.balance - withdraw
> def getbalance(self, balance):
> self.balance = bal + deposit - withdraw
> return self.balance
>
> (formal code)
> from account1a import Account
> from datetime import date
>
> now = date.today()
>
> a = Account()
>
> bal = input("Enter amount of starting balance: $")
> dpst = input("Enter amount of deposit: $")
> wdrw = input("Enter amount of withdrawal: $")
>
> print "As of",now,"balance is $",a.getbalance()
>
This piece of code looks very strange to me. Please don't misunderstand, but I
recommend you to gain more information about object oriented programming.
I would guess that the getbalance method returns the actual balance. So it
should not need any parameter (besides "self") and it could look like:
      def getbalance(self):
          return self.balance
Your implementation ignores the given parameter "balance", instead it tries to
compute something with values that do not exist. If you mean to use the
variables from outside the class/instance you would need to add "global
deposit, withdraw". Anyway my recommendation is not to that, because this
would not be "nice OO programming". I suggest something like the following
lines (written from scratch, untested):

 class Account:
      def __init__(self, balance=0): # make the initial balance optional, now
you can call either a = Account (42) or simply a=Account () which sets the
initial balance to 0
          self.balance = balance
      def deposit(self, deposit):
          self.balance = self.balance + deposit
      def withdraw(self, withdraw):
          self.balance = self.balance - withdraw
      def getbalance(self):
          return self.balance

 from account import Account
 from datetime import date

 bal = input("Enter amount of starting balance: $")
 a = Account (bal)
 dpst = input("Enter amount of deposit: $")
 a.deposit (dpst)
 wdrw = input("Enter amount of withdrawal: $")
 a.withdraw (wdrw)

 print "As of %s balance is $%f" % (date.today(), a.getbalance())

-Alex

> Appreciate all the assistance I might get.
>
>
>
>
>
>
>
> "Larry Bates" <lbates@swamisoft.com> wrote in message
> news:<oKKdnUmLvus3j6PcRVn-gQ@comcast.com>...
>
> > You really should post what you have tried and
> > any traceback error messages of things that didn't
> > work. A little background explanation of what you
> > are trying to do would also be nice. It's impossible
> > for us to venture a guess as to what you might be
> > doing wrong or to suggest a methodology without this
> > information.
> >
> > Regards,
> > Larry Bates
> > Syscon, Inc.
> >
> > "Crypt Keeper" <crypt_keeper@rome.com> wrote in message
> > news:29179565.0409071136.4147591f@posting.google.com...
> >
> > > I am trying to write a program that asks user for several input items,
> > > then takes those items into a class that will manipulate the data and
> > > then return a line of output. I am able to input the reuired
> > > information, but it's the in-class processing and output line that
> > > keeps messing up somehow. I have tried tinkering and tweaking but with
> > > no success.
> > >
> > > How can I take data from the user, manipulate it through a class of
> > > steps and then output it all into a line of output?
> > >
> > > Thanks!!



Relevant Pages

  • Re: Vb Help!!
    ... > transaction, deposit, check, or service charge. ... > the user to enter the amount of the trans. ... Calculate the balance by adding the deposits and subtacting ...
    (microsoft.public.vb.general.discussion)
  • Re: writing a class
    ... initial starting balance, amount of deposits, amount of withdrawals. ... from Account import Account ...
    (comp.lang.python)
  • Re: Newbie Question: What is a class for?
    ... One way to think of a class is as a data type and operations on that data type. ... For example, consider a checking account. ... (a balance), and operations: deposit, withdraw, write_check, ... def deposit amount ...
    (comp.lang.ruby)
  • Re: "In Todays Dollars" help needed...
    ... The zero takes account that no payments are made each year, ... >> Now this is the starting amount for next year, so after two years we have ... This spreadsheet is used to project ... >>>the balance in an account for the future. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: How do you calculate daily interest ?
    ... interest to the account on a fixed day each month. ... initial mortgage amount at the top and then have the a column going ... If your balance at the start ... that amount times 0.02052%, which comes to $300. ...
    (uk.finance)