首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于tkinter的问题,annualInterestRate未定义

问关于tkinter的问题,annualInterestRate未定义
EN

Stack Overflow用户
提问于 2019-04-04 06:32:14
回答 1查看 38关注 0票数 0

我需要一些帮助我的代码,我的任务是计算每月付款,并与给定的利息总付款。用户输入贷款金额和年份。然后,程序使用tkinter在窗口中显示付款,直到利息达到8.0。

我的问题是,我不能让我的程序显示利息,它只显示0.0,但给我一个错误"annualInterestRate is not defined",程序显示每月和总付款很好,但只显示第一行,不继续显示其余的付款。

我真的是编程新手,所以任何技巧都很感谢。

下面是完整文件的链接:https://pastebin.com/AUicQzu0

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-04-05 11:12:48

在Calculate的最后一行中,您尝试从变量annualInterestRate中获取值并将其赋值给self.annualInterestRateVar

代码语言:javascript
复制
self.annualInterestRateVar.set(annualInterestRate)

但annualInterestRate是只存在于Calculate中的局部变量,与存在于getMonthlyPayment中的局部变量annualInterestRate无关

因此,您尝试从变量annualInterestRate中获取值,但是在Calculate中没有为annualInterestRate赋值。

对于Python,您尝试从从未创建的变量中获取值。所以Python显示

代码语言:javascript
复制
 annualInterestRate is not defined

我不知道你在self.annualInterestRateVar.set(...)中想要什么价值。如果它是存在于getMonthlyPayment中的局部变量annualInterestRate的值,那么也许您应该从getMonthlyPayment返回它

代码语言:javascript
复制
return monthlyPayment, annualInterestRate

和在Calculate中的assing

代码语言:javascript
复制
monthlyPayment, annualInterestRate = self.getMonthlyPayment(...)

顺便说一句:你在getMonthlyPayment的while循环中有错误的缩进,它将在第一次循环后退出getMonthlyPayment。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55505289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档