Reputation: 47
I am making a program that uses the class Account to print the monthly interest amount of accountA, among other things. I am having problems with getting the getMonthlyInterestRate() and getMonthlyInterest definitions to work out. Here is the program thus far:
Account.py
class Account:
def __init__(self,id=0,balance=100.0,annualInterestRate=0.0):
self.__id=id
self.__balance=balance
self.__annualInterestRate=annualInterestRate
def getId(self):
return self.__id
def getBalance(self):
return self.__balance
def getAnnualInterest(self):
return self.__annualInterestRate
def setId(self,newid):
self.__id=newid
def setBalance(self,newbalance):
self.__balance=newbalance
def setAnnualInterestRate(self,newannualInterestRate):
self.__annualInterestRate=newannualInterestRate
def getMonthlyInterestRate(self,getAnnualInterest):
return(getAnnualInterest(self)/12)
def getMonthlyInterest(self,getBalance,getMonthly):
return(getBalance(self)*getMonthlyInterestRate(self))
def withdraw(self,amount):
if(amount<=self.__balance):
self.__balance=self.__balance-amount
def deposit(self,amount):
self.__balance=self.__balance+amount
def __str__(self):
return "Account ID : "+str(self.__id)+" Account Balance : "+str(self.__balance)+" Annual Interest Rate : "+str(self.__annualInterestRate)
next
file test.py
from Account import Account
def main():
accountA=Account(0,100,0)
accountA.setId(1234)
accountA.setBalance(20500)
accountA.setAnnualInterestRate(0.375)
print(accountA.__str__())
accountA.withdraw(500)
accountA.deposit(1500)
print(accountA.__str__())
print(accountA.getMonthlyInterest(accountA.getBalance(),accountA.getAnnualInterest()))
main()
I cannot figure out how to make the getMonthlyInterestRate() and getMonthlyInterest() defintions to work out to be able to put out the right output, which is:
Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375
Monthly Interest Amount : 671.875
mine always comes out with the error statement:
Account ID : 1234 Account Balance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Balance : 21500 Annual Interest Rate : 0.375
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 13, in <module>
File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 12, in main
File "C:\Users\Meagan\Documents\University\2nd Year\Cmput 174\Account.py", line 21, in getMonthlyInterest
return(getBalance(self)*getMonthlyInterestRate(self))
builtins.TypeError: 'int' object is not callable
this is what i should make:
a method named getMonthlyInterestRate() that returns the monthly interest rate.
a method named getMonthlyInterest() that return the monthly interest amount. The monthly interest amount can be calculated by using balance * monthly interest rate. The monthly interest rate can be computed by dividing the annual interest rate by 12.
everything else in the program is correct except for those two definitions and the last print statement. Any help would be appreciated. Thanks.
Upvotes: 0
Views: 2701
Reputation: 1122372
You should call methods on self
, not by passing the functions around:
def getMonthlyInterest(self):
return self.getBalance() * self.getMonthlyInterestRate()
and call it with:
print(accountA.getMonthlyInterest())
This goes for getMonthlyInterestRate
as well:
def getMonthlyInterestRate(self):
return self.getAnnualInterest() / 12
You use a lot of getters and setters; there is no need for these in Python; you don't need to make the attributes private, just access them directly instead:
class Account:
def __init__(self, id=0, balance=100.0, annualInterestRate=0.0):
self.id = id
self.balance = balance
self.annualInterestRate = annualInterestRate
def getMonthlyInterestRate(self):
return self.annualInterestRate / 12
def getMonthlyInterest(self):
return self.balance * self.getMonthlyInterestRate()
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def __str__(self):
return "Account ID : {0.id} Account Ballance : {0.balance} Annual Interest Rate : {0.annualInterestRate}".format(self)
then run:
def main():
accountA = Account(0,100,0)
accountA.id = 1234
accountA.balance = 20500
accountA.annualInterestRate = 0.375
print(accountA)
accountA.withdraw(500)
accountA.deposit(1500)
print(accountA)
print(accountA.getMonthlyInterest())
Result:
Account ID : 1234 Account Ballance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Ballance : 21500 Annual Interest Rate : 0.375
671.875
Upvotes: 5
Reputation: 91059
You define
def getMonthlyInterestRate(self,getAnnualInterest):
return(getAnnualInterest(self)/12)
def getMonthlyInterest(self,getBalance,getMonthly):
return(getBalance(self)*getMonthlyInterestRate(self))
and use them as
print(accountA.getMonthlyInterest(accountA.getBalance(),accountA.getAnnualInterest()))
If you fix this bug, you (probably) make your program work, but youget a program written in very bad style.
To improve that, follow Martijn Pieters's hint.
(This answer should probably have been a comment, but these cannot be formatted nicely.)
Upvotes: 0