2016-11-30 51 views
1
# Create a class called "Loan": 
# Data fields in the Loan class include: Annual Interest Rate(Float),\ 
# Number of years of loan(Float), Loan Amount(Float), and Borrower's Name(string) 
class Loan: 
    # Create the initializer or constructor for the class with the above data fields. 
    # Make the data fields private. 
    def __init__(self, annualInterestRate, numberOfYears, loanAmount, borrowerName): 
     self.__annualInterestRate=annualInterestRate 
     self.__numberOfYears=numberOfYears 
     self.__loanAmount=loanAmount 
     self.__borrowerName 
    # Create accessors (getter) for all the data fields: 
    def getannualInterestRate(self): 
     return self.__annualInterestRate 
    def getnumberOfYears(self): 
     return self.__numberOfYears 
    def getloanAmount(self): 
     return self.__loanAmount 
    def getborrowerName(self): 
     return self.__borrowerName 
    # Create mutators (setters) for all the data fields: 
    def setannualInterestRate(self): 
     self.__annualInterestRate=annualInterestRate 
    def setnumberOfYears(self): 
     self.__numberOfYears=numberOfYears 
    def setloanAmount(self): 
     self.__loanAmount=loanAmount 
    def setborrowerName(self): 
     self.borrowerName=borrowerName 
    # Create a class method: getMonthlyPayment - 
    def getMonthlyPayment(self,loanAmount, monthlyInterestRate, numberOfYears): 
     monthlyPayment = loanAmount * monthlyInterestRate/(1 
     - 1/(1 + monthlyInterestRate) ** (numberOfYears * 12)) 
     return monthlyPayment; 
    # Create a class method: getTotalPayment - 
    def getTotalPayment(self): 
     monthlyPayment = self.getMonthlyPayment(float(self.loanAmountVar.get()), 
     float(self.annualInterestRateVar.get())/1200, 
     int(self.numberOfYearsVar.get())) 
     self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f')) 
     totalPayment = float(self.monthlyPaymentVar.get()) * 12 \ 
     * int(self.numberOfYearsVar.get()) 
     self.totalPaymentVar.set(format(totalPayment, '10.2f')) 

def main(): 
    loan1=Loan() 
    print(input(float("Enter yearly interest rate, for exmaple, 7.25: ", loan1.annualInterestRate()))) 
    print(input(float("Enter number of years as an integer: ", loan1.getnumberOfYears()))) 
    print(input(float("Enter loan amount, for example, 120000.95: ", loan1.getloanAmount()))) 
    print(input(float("Enter a borrower's name: ", loan1.getborrowerName()))) 

    print("The loan is for", loan1.getborrowerName()) 
    print("The monthly payment is", loan1.getMonthlyPayment()) 
    print("The total payment is", loan1.getTotalPayment()) 

    print(input("Do you want to change the loan amount? Y for Yes OR Enter to Quit")) 

    print(input(float("Enter a new loan amount: "))) 
    print("The loan is for", loan1.getborrowerName()) 
    print("The monthly payment is", loan1.getMonthlyPayment()) 
    print("The total payment is", loan1.getTotalPayment()) 

main() 

由于某种原因,我的程序未运行。 我试图让用户更改贷款金额并重新打印新的贷款信息。我不知道自己做错了什么,而班级/面向对象对我来说是新的,所以我正在努力工作,因为我一直在做程序 - 仅在过去的一年。我知道这是充满无数错误 ...但我无处开始。所有在线课程的教程都非常模糊和理论化,并且没有提及我所面对的具体示例/场景。贷款计算器使用类(OOP)不在Python中运行

任何帮助,将不胜感激。

+0

您定义的贷款构造需要4个ARGS。你不用任何参数定义贷款。 – Fallenreaper

+0

我把'贷款()'在主要'贷款(自我,annualInterestRate,numberOfYears,loanAmount,borrowerName)',它给了我错误:'“自我没有定义”'... – pyglasses

+1

你不需要在调用它时将'self'传递给一个类方法,您只需在定义类方法时包含它。请在这里阅读更多:https://docs.python.org/3/tutorial/classes.html – Jarvis

回答

1

解释器生成的错误信息绰绰有余。

TypeError: __init__() takes exactly 5 arguments (1 given) 

你需要传递任何输入您从用户考虑作为参数传递到类的构造函数Loan()。其他方法仅用于返回类变量,但所有初始化都是在构造函数中完成的。

而且,你的构造函数定义是错误的,正确的这一行:

self.__borrowerName=borrowerName 
+0

评论不适合长时间讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/129427/discussion-on-answer-by-jarvis-loan-calculator-using-classoop-not-running-in-p) 。 –