Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;“对分搜索”;麻省理工学院_Python_Bisection - Fatal编程技术网

Python &引用;“对分搜索”;麻省理工学院

Python &引用;“对分搜索”;麻省理工学院,python,bisection,Python,Bisection,我碰巧发现了这段代码,它似乎运行得很好。令人惊讶的是,(通过了麻省理工学院课程的检查)只有当年利率为0.15时,它才会失败,其他情况也一样。我是个新手,所以我今天没有任何希望解决这个问题,但是如果有人能给我一些关于这个问题的信息,我将非常感激 balance=294272 annualInterestRate=0.15 payment=(balance * (1 + annualInterestRate/12)**12) / 12 count=0 total=0 inicialbalance=

我碰巧发现了这段代码,它似乎运行得很好。令人惊讶的是,(通过了麻省理工学院课程的检查)只有当年利率为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)))`
我认为情况

if balance <=0:
    break
屈服

Payment at 42279.7094717 ending balance : -43172.4850925
Payment will adjust to: 38682.0023807
Payment at 38682.0023807 ending balance : 3673.67604215
Payment will adjust to: 38988.1420508
Payment at 38988.1420508 ending balance : -312.604095728
Payment will adjust to: 38962.0917095
Payment at 38962.0917095 ending balance : 26.6004186392
Payment will adjust to: 38964.3084111
Payment at 38964.3084111 ending balance : -2.26350928031
Payment will adjust to: 38964.1197853
Payment at 38964.1197853 ending balance : 0.192608783082
Payment will adjust to: 38964.1358361
Payment at 38964.1358361 ending balance : -0.0163896580426
Payment will adjust to: 38964.1344702
Lowest Payment: 38964.13

它如何“失败”?您是否得到错误或错误的结果?哪个错误?对不起,我应该把问题说清楚。除了少数利率(年利率)为0.15的案例外,我对所有随机测试案例都得到了正确的结果。我不觉得有什么意义,但它是这样的:(好的,那么你得到了什么结果?你期望得到什么?你自愿提供的信息越多,就越有可能有人能提供帮助……你应该在代码中插入一堆
print
语句来找出问题所在。例如,对于balance=32000和a.i.r=0.2,它的输出为29157.09,这是正确的。然而,对于balan来说ce=437092和AnnualInterestate=0.15正确答案是38964.14,而我的答案是42249.37。这是一个很大的差异,太大了,无法与其他案例的结果拟合得那么好
balance = 437092
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) + interest
        total += payment
        finalbalance = balance-total
    if balance <-0.01 or balance>0.01:
        print('Payment at ' + str(payment) + ' ending balance : ' + str(balance))
        payment=payment+(balance/12)
        print('Payment will adjust to: ' + str(payment))
        count=count+1
        balance=inicialbalance
    else:
        break
    total=0
print('Lowest Payment: ' + str(round(payment,2)))
Payment at 42279.7094717 ending balance : -43172.4850925
Payment will adjust to: 38682.0023807
Payment at 38682.0023807 ending balance : 3673.67604215
Payment will adjust to: 38988.1420508
Payment at 38988.1420508 ending balance : -312.604095728
Payment will adjust to: 38962.0917095
Payment at 38962.0917095 ending balance : 26.6004186392
Payment will adjust to: 38964.3084111
Payment at 38964.3084111 ending balance : -2.26350928031
Payment will adjust to: 38964.1197853
Payment at 38964.1197853 ending balance : 0.192608783082
Payment will adjust to: 38964.1358361
Payment at 38964.1358361 ending balance : -0.0163896580426
Payment will adjust to: 38964.1344702
Lowest Payment: 38964.13