关于tkinter的问题,年度利益未定义

2024-10-01 04:47:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些帮助,我的代码,我的任务是计算每月付款,总付款与给定的利息。用户输入贷款金额和年数。然后,程序使用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

Tags: self程序getdeffloatintset利息
1条回答
网友
1楼 · 发布于 2024-10-01 04:47:14

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

在{}中分配

monthlyPayment, annualInterestRate = self.getMonthlyPayment(...)

顺便说一句:您在getMonthlyPaymentwhile循环中有错误的缩进,它将在第一个循环后退出getMonthlyPayment

相关问题 更多 >