0

我的代码:

monthlyInterestRate = annualInterestRate/12.0 
low = balance/12 
high = (balance*(1+monthlyInterestRate)**12)/12 
guess = (low+high)/2 
unpaidBalance = balance 
month = 1

while True:
    unpaidBalance= unpaidBalance-guess 
        while month < 13:
            if unpaidBalance <= -0.1:
                low = guess
                month += 1
            elif unpaidBalance >= 0.1:
                high = guess
                month += 1
            else:
                break
            guess = (low + high)/2 
print "Lowest Payment: " + str(round(guess, 2))

当我测试它时,它卡在“当月 < 13:”这一行

为什么会这样,我该如何解决?

4

2 回答 2

1

如果你在每个内部 while 循环处中断,你仍然小于 13。

自从您继续While True并且不更新您的guess.

我担心你在那里面临无限循环。

您的break语句打破了最近的循环,即While month < 13循环。下一行不读。guess没有更新。while True不坏。

也许你想说

while month < 13:
   unpaidBalance= unpaidBalance-guess 
   if unpaidBalance <= -0.1:
         low = guess
   elif unpaidBalance >= 0.1:
         high = guess
   month += 1
   guess = (low + high)/2 
于 2013-02-25T20:30:39.627 回答
1

这个给你

否是最好的解决方案,但它有效

monthlyPaymentRate = (balance*annualInterestRate/12)/((1-(1+annualInterestRate/12)**-12))

interest = monthlyPaymentRate * (annualInterestRate/12)

#print (monthlyPaymentRate)
#print (interest)
monthlyPaymentRate = (monthlyPaymentRate - interest) +1
#print (monthlyPaymentRate)
balanceInit = balance

epsilon = 0.01
low = monthlyPaymentRate
while low*12 - balance > epsilon:
    balances = balanceInit
    for i in range(12):
                 minpay =  monthlyPaymentRate
                 unpaybal = balances - minpay
                 interest = (annualInterestRate /12) * unpaybal
                  smontfinal =  unpaybal + interest
                  balances = smontfinal
                 #print('Remaining balance: ' ,round(balances,2) )
                 if balances <0:
                     low = -1
                     break
    if balances < 0 :
        low = -1
    else:
        monthlyPaymentRate =monthlyPaymentRate + 0.001 

print('Lowest Payment:' ,round(monthlyPaymentRate,2) )
于 2016-09-27T21:26:39.640 回答