Python Bisection搜索

时间:2017-06-21 02:45:35

标签: python python-3.x bisection

我是Python新手,目前正在攻读MIT edX课程。我们有一个问题需要完成,我们必须使用二分法来找到一个人在一年内偿还信用卡部门所需的最低金额。我们获得了余额和年利率。我的代码在下面有效,但对我来说看起来不正确。有没有人对此有任何见解?感谢

def payment(balance, annualInterestRate):
    newBalance = 0
    monthlyInterest = annualInterestRate / 12
    maxPaybal = (balance * (1 + monthlyInterest) ** 12) / 12
    minPaybal = balance/12
    while round(newBalance, 2) != 0.01 or round(newBalance, 2) != 0.00:
            guess = (minPaybal + maxPaybal) / 2.0
            newBalance = balance
            months = 12
            while months > 0:
                prevBalance = newBalance - guess
                uptdBalance = prevBalance + (prevBalance * monthlyInterest)
                newBalance = uptdBalance
                months -= 1
            if round(newBalance, 2) == 0.01 or round(newBalance, 2) == 0.00:
                return "Lowest Payment: ", round(guess, 2)
            elif newBalance < 0.00:
                maxPaybal = guess
            else:
                minPaybal = guess
print(payment(balance, annualInterestRate))

0 个答案:

没有答案