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