简体   繁体   English

在Python中使用对分法计算付款金额

[英]Calculating Payment Size Using Bisection Method in Python

I've been working on this problem for MIT's edX course. 我一直在为MIT的edX课程解决这个问题。 The goal of the problem is to calculate the payment size for one year given the loan size and APY. 问题的目的是给定贷款额和APY,计算一年的还款额。 My problem is that my answer keeps coming up too high. 我的问题是我的答案一直很高。 I'm not exactly sure why this is. 我不确定为什么会这样。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the code: 这是代码:

b = float(input("balance = "))
r = float(input("annualInterestRate = "))
t = float(input("How many months do you want to take = "))
p = (b / t)
bval = []
new = b


def interest(b, r, p, t):

    bal = (b - p)
    max = (b * (1 + r / 12)**(12))/12
    min = (b / t)

    def update(bal, r):
        balance = (bal + (r / 12.0) * bal)
        return balance

    if len(bval) < t:
        bval.append(update(bal, r))
        return(interest(bval[-1], r, p, t))

    if (len(bval) == t):
        if bval[-1]< 10:
            return print(" Minimum payment: %i" % p)

        p = (max + min) / 2.0

        if bval[-1] < 0:
            min = bval[-1]
        elif bval[-1] > 0:
            max = bval[-1]

        bval.clear()
        bval.append(update((new - p), r))
        return(interest(bval[-1], r, p, t))

interest(b, r, p, t)

There may be an issue with your order of operations in max. 您的最高操作顺序可能有问题。 You might want to try adding parentheses around 1+r before dividing it by 12. 您可能想要在将其除以12之前尝试在1 + r周围添加括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM