麻省理工学院的“平分搜索”

2024-05-23 12:29:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我碰巧发现了这个代码,看起来运行良好。令人惊讶的是,(通过了麻省理工学院课程的检查)只有当年利率为0.15时,它才失败,其他情况下也没问题。我是个新手,所以我今天没有希望解决这个问题,但是如果有人能给我一些启示,我会非常感激!

balance=294272
annualInterestRate=0.15

payment=(balance * (1 + annualInterestRate/12)**12) / 12
count=0
total=0
inicialbalance=balance
while balance>0:
    for month in range(1,13):
        interest=(balance-payment)*(annualInterestRate/12)
        balance=(balance-payment)*(1+(annualInterestRate/12))
        total=total+payment
        finalbalance=balance-total
        if balance <=0:
            print('Balance Paid Off @ '+str(round(payment,2)) + ' Total Paid is:       '+str(round(total,2))+ ' ending balance: ' +str(round(balance,2))) 
            print('Lowest Payment: ' + str(round(payment,2)))
            break
    if balance <-0.01 or balance >0.01:
        print('Payment at ' + str(payment) + ' ending balance : ' + str(balance))
        print('Payment will adjust to: ' + str(payment + 10))
        payment=payment+(balance/12)
        count=count+1
        balance=inicialbalance
    total=0
    #if count >200:
        #print('count is > ' +str(count)+ ' aborting')
        #break
    #if balance <=0:
        #print('balance paid off at ' + str(payment) + ' after ' + str(mth))
print('Lowest Payment: ' + str(round(payment,2)))`

结果:

{Balance Paid Off @ 42279.71 Total Paid is: 465076.8 ending balance: -359.78
Lowest Payment: 42279.71
Payment at 42279.7094717 ending balance : -359.781977705
Payment will adjust to: 42289.7094717
Balance Paid Off @ 42249.73 Total Paid is: 464747.0 ending balance: -4.19
Lowest Payment: 42249.73
Payment at 42249.7276402 ending balance : -4.18662043777
Payment will adjust to: 42259.7276402
Balance Paid Off @ 42249.38 Total Paid is: 464743.17 ending balance: -0.05
Lowest Payment: 42249.38
Payment at 42249.3787552 ending balance : -0.0487178117574
Payment will adjust to: 42259.3787552
Balance Paid Off @ 42249.37 Total Paid is: 464743.12 ending balance: -0.0
Lowest Payment: 42249.37
balance paid off at 42249.3746954 after 11
Lowest Payment: 42249.37}

Tags: iscountendingpaymentattotalprintbalance