我需要一些帮助,我的代码,我的任务是计算每月付款,总付款与给定的利息。用户输入贷款金额和年数。然后,程序使用tkinter在窗口中显示付款,直到利息达到8.0
我的问题是,我不能让我的程序显示利息,它只显示0.0,但给我错误“AnnualInterestate未定义”,程序显示每月和总付款只是罚款,但只有第一行,不继续显示其余的付款
我真的是编程新手,所以任何提示都非常感谢
下面是指向完整文件的链接:https://pastebin.com/AUicQzu0
def Calculate(self):
monthlyPayment = self.getMonthlyPayment(
float(self.loanamountVar.get()),
int(self.yearsVar.get()),
float(self.annualInterestRateVar.get()))
self.monthlyPaymentVar.set(format(monthlyPayment, '10.2f'))
totalPayment = float(self.monthlyPaymentVar.get()) * 12 * int(self.yearsVar.get())
self.totalPaymentVar.set(format(totalPayment, '10.2f'))
self.annualInterestRateVar.set(annualInterestRate)
def getMonthlyPayment(self, loanamount, years, annualInterestRate):
annualInterestRate = 5.0
while annualInterestRate <= 8.0:
monthlyInterestRate = annualInterestRate / 1200
monthlyPayment = loanamount * monthlyInterestRate / (1 - 1 / (1 + monthlyInterestRate) ** (years * 12))
annualInterestRate += 1.0 / 8
return monthlyPayment
在
Calculate
的最后一行中,您尝试从变量annualInterestRate
中获取值并分配给self.annualInterestRateVar
但是
annualInterestRate
是只存在于Calculate
中的局部变量,与存在于getMonthlyPayment
中的局部变量annualInterestRate
无关因此,您试图从变量
annualInterestRate
中获取值,但没有为Calculate
中的annualInterestRate
赋值对于Python,您尝试从从未创建的变量中获取值。所以Python显示
我不知道你试图在
self.annualInterestRateVar.set(...)
中赋予什么价值。如果它是存在于getMonthlyPayment
中的局部变量annualInterestRate
的值,那么您可能应该从getMonthlyPayment
返回它在{}中分配
顺便说一句:您在
getMonthlyPayment
的while
循环中有错误的缩进,它将在第一个循环后退出getMonthlyPayment
相关问题 更多 >
编程相关推荐