# 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对我来说是新的,所以在过去一年中,我只做程序性工作,所以我一直在努力挣扎。我知道这充满了很多错误...但是我无处可去。所有在线课程的教程都是非常模糊和理论化的,没有像我所面对的那样具体示例/场景。

任何帮助,将不胜感激。

最佳答案

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

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


您需要将从用户那里获取的所有输入作为参数传递给类构造函数Loan()。其他方法仅用于返回类变量,但是所有初始化都在构造函数中完成。

另外,您的构造函数定义有误,请更正此行:

self.__borrowerName=borrowerName

关于python - 使用class(OOP)的贷款计算器未在python中运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40879240/

10-09 19:07