473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1904
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**********@r ome.com> wrote in message
news:29******** *************** ***@posting.goo gle.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!!

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,"balanc e 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\Mo dule1a.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,"balanc e is $",a.getbalance ()
Appreciate all the assistance I might get.



"Larry Bates" <lb****@swamiso ft.com> wrote in message news:<oK******* *************@c omcast.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**********@r ome.com> wrote in message
news:29******** *************** ***@posting.goo gle.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!!

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,"balanc e 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\Mo dule1a.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,"balanc e 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****@swamiso ft.com> wrote in message
news:<oK******* *************@c omcast.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**********@r ome.com> wrote in message
news:29******** *************** ***@posting.goo gle.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!!

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+=a mt

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(initi al=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("Ent er 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(initi al=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(twdr w)
except:
print "Bad withdrawal=%s" % twdrw
print "You must enter withdrawals like 100.00"
continue

badtwdrw=0

a.witdraw(wdrw)

print "As of",now,"balanc e 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**********@r ome.com> wrote in message
news:29******** *************** ***@posting.goo gle.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,"balanc e 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\Mo dule1a.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,"balanc e is $",a.getbalance ()
Appreciate all the assistance I might get.



"Larry Bates" <lb****@swamiso ft.com> wrote in message

news:<oK******* *************@c omcast.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**********@r ome.com> wrote in message
news:29******** *************** ***@posting.goo gle.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!!

Jul 18 '05 #5

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

Similar topics

11
2225
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 small, simply objects was an education for me. What I was wondering, when I rewrote my cms from procedural code to OO was how to avoid end up have every class called by ever other? I was writing massive objects where every object needed every...
6
23599
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
2080
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 or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
2
6783
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 simple calculations to work out from my integer what the individual values of the four bytes have to be. Surely there must be a short cut to doing this? And are there also any existing routines for converting a floating point number into...
24
3335
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 use the header file of the class developed by me to access its functionality. so what should be there in the header file? Only the public methods declaration / data members of that class or the whole layout of the class along with its all private...
6
2175
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 and/or processes so that multiple threads/processes can write to the same file (all threads use the same instance of the stream, processes use a different instance but still may point to the same file) Could you point me in the correct...
9
2095
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, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
1
2967
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 : Defines the entry point for the console application. #include "stdafx.h" #include <stdio.h> #include <conio.h> #include <iostream>
89
3821
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 record inheritance (records do have operator overloading) The idea is to write a generic integer class with derived integer classess for 8 bit, 16 bit, 32 bit, 64 bit and 64 bit emulated.
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.