首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -找到信用卡的每月净现值

Python -找到信用卡的每月净现值
EN

Stack Overflow用户
提问于 2015-06-23 14:17:01
回答 1查看 151关注 0票数 0

我在运行这个程序:

代码语言:javascript
复制
balance = 320000
annualInterestRate = 0.2

monthlyPayment = 0.0
monthlyInterestRate = annualInterestRate/12
low = balance / 12.0
high = ((balance * ((1.0 + monthlyInterestRate)**12.0))/12.0)
monthlyPayment = (high+low)/2
while balance != 0:
    newBalance = balance
    for x in range(1, 13):
        lastMonthBalance = newBalance - monthlyPayment
        newBalance = lastMonthBalance + ((monthlyInterestRate) * lastMonthBalance)
        if -0.002 <= newBalance <= 0.002:
            balance = 0

    if newBalance < 0.0:
        low = 0
        high = monthlyPayment
    elif newBalance > 0.0:
        low = monthlyPayment

    monthlyPayment = (low + high) / 2

print "Lowest Payment: %.2f" % monthlyPayment

最初,我试图找到使我的newBalance等于0的月付款,但是由于处理时间的限制,我选择了一种不太准确的方法。

然而,当我把界限从-0.2/0.2改为-0.002/0.002时,我的答案从14578.55变为29157.09,我不明白为什么?

任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-23 16:13:27

因此,我不完全确定为什么,但通过删除low = 0的行,代码才能正常工作。

工作守则是:

代码语言:javascript
复制
#Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.

balance = 999999
annualInterestRate = 0.18

monthlyPayment = 0.0
monthlyInterestRate = annualInterestRate/12
low = balance / 12.0
high = ((balance * ((1.0 + monthlyInterestRate)**12.0))/12.0)
monthlyPayment = (high+low)/2
while balance != 0:
    newBalance = balance
    for x in range(1, 13):
        lastMonthBalance = newBalance - monthlyPayment
        newBalance = lastMonthBalance + ((monthlyInterestRate) * lastMonthBalance)
        if -0.05 <= newBalance <= 0.05:
            balance = 0

    if newBalance < 0.0:
        high = monthlyPayment
    elif newBalance > 0.0:
        low = monthlyPayment

    monthlyPayment = (low + high) / 2

print "Lowest Payment: %.2f" % monthlyPayment

如果有人能向我解释为什么会发生这种情况,我将非常感激。

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

https://stackoverflow.com/questions/31005317

复制
相关文章

相似问题

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