writing a class

C

Crypt Keeper

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!!
 
L

Larry Bates

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.
 
C

Crypt Keeper

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.
 
A

Alexander Hoffmann

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
 
L

Larry Bates

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top