473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

writing a class

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!!
Jul 18 '05 #1
4 1892
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" <cr**********@rome.com> wrote in message
news:29**************************@posting.google.c om...
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!!

Jul 18 '05 #2
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" <lb****@swamisoft.com> wrote in message news:<oK********************@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" <cr**********@rome.com> wrote in message
news:29**************************@posting.google.c om...
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!!

Jul 18 '05 #3
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" <lb****@swamisoft.com> wrote in message
news:<oK********************@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" <cr**********@rome.com> wrote in message
news:29**************************@posting.google.c om...
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!!

Jul 18 '05 #4


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" <cr**********@rome.com> wrote in message
news:29**************************@posting.google.c om...
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" <lb****@swamisoft.com> wrote in message

news:<oK********************@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" <cr**********@rome.com> wrote in message
news:29**************************@posting.google.c om...
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!!

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: lawrence | last post by:
I asked a lot of questions in May about how to organize OO code. I didn't get great answers here, but someone here suggested that I look the Eclipse library, which was a good tip. Looking at its...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
2
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
1
by: Smita Prathyusha | last post by:
I am facing a problem in writing to COM1. I am using a Win 32 Console mode Program in VC++ the following is the code: If anyone can help me out it will be of great help : // SC_Using_Classes.cpp...
89
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.