2016-11-30 3 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は私には新しかったので、私は過去1年間手続きのみを行ってきたので心から悩んでいます。私はこれが多くのエラーで満たされていることを知っています...しかし、私はどこにも始まらない。オンラインのクラスのチュートリアルはすべて、あまりにも曖昧かつ理論的なものであり、私が直面しているような具体的な事例/シナリオを紹介していません。Pythonで実行されていないクラス(OOP)を使用したローン計算機

ご協力いただければ幸いです。

+0

はまた、あなたのコンストラクタの定義は、この行を修正し、間違っています。あなたはあなたのメインのargsでLoanを定義しません。 – Fallenreaper

+0

メインの 'Loan()'を 'Loan(self、annualInterestRate、numberOfYears、loanAmount、borrowerName)'に変更しました。エラーが表示されました: '' self is not defined ''... – pyglasses

+1

あなたはクラスメソッドに 'self 'を渡すときは、クラスメソッドを定義するときにそれをインクルードする必要があります。詳しくはこちらをご覧ください:https://docs.python.org/3/tutorial/classes.html – Jarvis

答えて

1

インタープリタによって生成されたエラーメッセージは十分です。

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

あなたはクラスのコンストラクタLoan()への引数としてユーザから取っているものは何でも入力渡す必要があります。他のメソッドはクラス変数を返すためのものですが、すべての初期化はコンストラクタで行われます。あなたが4つの引数を必要とするローンのコンストラクタを定義

self.__borrowerName=borrowerName 
+0

コメントは議論の延長ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/129427/discussion-on-answer-by-jarvis-loan-calculator-using-classoop-not-running-in-p) 。 –

関連する問題