writing a class

Larry Bates lbates at swamisoft.com
Thu Sep 9 09:41:09 EDT 2004



With a few changes this will work.

from datetime import date
import sys

class Account:
     #
     # Make initial a keyword argument so that it is
     # optional.  If not given, begin with zero.
     #
     def __init__(self, initial=0.0):
         self.balance = initial

     def deposit(self, amt):
         #
         # You were referencing global balance
         # (not self.balance) before.
         #
         self.balance+=amt

     def withdraw(new, amt):
         self.balance-=amt

     def getbalance(self):
         return self.balance

if __name__== "__main__":

#
# Main program begins here
#
a = Account() # or a=Account(initial=0.0)
now = date.today()

#
# Loop until you get a good starting balance
#
badtbal=1
while badtbal:
    #
    # Input comes in as text, must try to convert to
    # a float.
    #
    tbal=input("Enter amount of starting balance (blank to stop): $")
    if not tbal: sys.exit(0)
    try: bal=float(tbal)
    except:
        print "Bad starting balance=%s" % tbal
        print "You must enter starting balance like 100.00"
        continue

    badtbal=0

a=Account(initial=bal)

badtdpst=1
while badtdpst:
    tdpst = input("Enter amount of deposit (blank to stop): $")
    if not tdpst: sys.exit(0)
    try: dpst=float(tbal)
    except:
        print "Bad deposit=%s" % tdpst
        print "You must enter deposits like 100.00"
        continue

    badtdpst=0

a.deposit(dpst)

badtwdrw=1
while badtwdrw:
    twdrw = input("Enter amount of withdrawal (blank to stop): $")
    if not wdrw: sys.exit(0)
    try: wdrw=float(twdrw)
    except:
        print "Bad withdrawal=%s" % twdrw
        print "You must enter withdrawals like 100.00"
        continue

    badtwdrw=0

a.witdraw(wdrw)

print "As of",now,"balance is $",a.getbalance()


As with most programming projects you need to include more
code to try to catch input errors from the user than to
actually solve the problem.  This wasn't test, but I'll
bet it is close to working.

Hope it helps,
Larry Bates
Syscon, Inc.


"Crypt Keeper" <crypt_keeper at rome.com> wrote in message
news:29179565.0409081015.5c7a8e9d at posting.google.com...
> 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)
>
>
>
> 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()
>
>
> Appreciate all the assistance I might get.
>
>
>
>
>
>
>
> "Larry Bates" <lbates at swamisoft.com> wrote in message
news:<oKKdnUmLvus3j6PcRVn-gQ at 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 at rome.com> wrote in message
> > news:29179565.0409071136.4147591f at 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!!





More information about the Python-list mailing list