2016-11-30 50 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) 。 –