使用python中的二分搜索查找每月最低债务

时间:2015-01-30 13:14:40

标签: python

我正在尝试使用以下方式找到偿还贷款的最低金额:

 initialBalance = 320000
 annualInterestRate = 0.2
 monthlyInterestRate = annualInterestRate/12
 lo = initialBalance/12.0
 hi = ((initialBalance*(1+(annualInterestRate/12))**12)/12.0)
 epsilon = 0.01
 payment = (lo+hi)/2.0

while abs((payment*12) - initialBalance) >=epsilon:
     balance = initialBalance-payment
     balance += balance*(annualInterestRate/12)
     for i in range(1,12):  
         balance -= payment
         balance += balance*(annualInterestRate/12) 
     if balance > 0:
        lo = payment
        payment =(lo+hi)/2
    else: 
        hi = payment
        payment =(lo+hi)/2
print'Lowest payment: ', str(round(payment,2))

当我尝试运行此程序时,程序进入无限循环,我不明白为什么会发生这种情况。谁能帮到我这里?我查看了所有其他类似的帖子,仍然不明白为什么我的代码不起作用。

2 个答案:

答案 0 :(得分:0)

最好是尝试使用下面的代码自己找到它,因为看起来很难弄清楚你在代码中想要做什么(提示:使用注释让你很容易,和其他人了解你的代码)

import time

# ----------------------------------------------------
# here you can have the rest of your code, variable declaration etc.
# ----------------------------------------------------

while abs((payment*12) - initialBalance) >=epsilon:
    balance = initialBalance-payment
    balance += balance*monthlyInterestRate

    for i in range(1,12):
        balance -= payment
        balance += balance*monthlyInterestRate

    if balance > 0:
        lo = payment
        payment =(lo+hi)/2

    else:
        hi = payment
        payment =(lo+hi)/2

    # You can modify the code below to track other variables if you feel like it.
    print '\n--------'
    print 'balance %s' % balance 
    print 'payment %s' % payment
    # Inserts a small delay (in seconds) between prints
    time.sleep(0.2)

您可以试验并查看哪些变量获得意外值并跟踪它们。

答案 1 :(得分:0)

您需要循环,直到更新余额的绝对值为< =您的epsilon

initialBalance = 320000
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate / 12
lo = initialBalance / 12.0
hi = ((initialBalance * (1 + (annualInterestRate / 12)) ** 12) / 12.0)
epsilon = 0.01
payment = (lo + hi) / 2.0
new_balance = initialBalance
# you want the abs of new_balance to be <= epsilon
while abs(new_balance) >= epsilon:
    # reset new_balance each time
    new_balance = initialBalance
    # loop for twelve months
    for i in range(1, 13):
        new_balance -= payment
        new_balance += monthlyInterestRate * new_balance
    # change low to high and vice versa based on whether we took too much or too little
    if new_balance > 0:
        lo = payment
    else:
        hi = payment
    # recalculate payment based on new high and low
    payment = (lo + hi) / 2


print('Lowest payment: ', str(round(payment, 2)))

使用您自己的代码,您需要在循环外检查abs(balance) >=epsilon设置balance = initialBalance

balance = initialBalance
while abs(balance) >=epsilon:
     balance = initialBalance-payment
     balance += balance* monthlyInterestRate
     for i in range(1,12):
         balance -= payment
         balance += balance*(annualInterestRate/12)
     if balance > 0:
        lo = payment
        payment =(lo+hi)/2
     else:
        hi = payment
        payment =(lo+hi)/2
print('Lowest payment: ', str(round(payment,2)))

使用while abs((payment*12) - initialBalance) >=epsilon:,您将与initialBalance 进行比较,包括任何兴趣,因此在计算的付款正确时,您的循环永远不会结束。