我需要一些帮助我的代码,我的任务是计算每月付款,并与给定的利息总付款。用户输入贷款金额和年份。然后,程序使用tkinter在窗口中显示付款,直到利息达到8.0。
我的问题是,我不能让我的程序显示利息,它只显示0.0,但给我一个错误"annualInterestRate is not defined",程序显示每月和总付款很好,但只显示第一行,不继续显示其余的付款。
我真的是编程新手,所以任何技巧都很感谢。
下面是完整文件的链接: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发布于 2019-04-05 11:12:48
在Calculate的最后一行中,您尝试从变量annualInterestRate中获取值并将其赋值给self.annualInterestRateVar
self.annualInterestRateVar.set(annualInterestRate)但annualInterestRate是只存在于Calculate中的局部变量,与存在于getMonthlyPayment中的局部变量annualInterestRate无关
因此,您尝试从变量annualInterestRate中获取值,但是在Calculate中没有为annualInterestRate赋值。
对于Python,您尝试从从未创建的变量中获取值。所以Python显示
annualInterestRate is not defined我不知道你在self.annualInterestRateVar.set(...)中想要什么价值。如果它是存在于getMonthlyPayment中的局部变量annualInterestRate的值,那么也许您应该从getMonthlyPayment返回它
return monthlyPayment, annualInterestRate和在Calculate中的assing
monthlyPayment, annualInterestRate = self.getMonthlyPayment(...)顺便说一句:你在getMonthlyPayment的while循环中有错误的缩进,它将在第一次循环后退出getMonthlyPayment。
https://stackoverflow.com/questions/55505289
复制相似问题