我的代码中有无限循环,我不知道它从何而来

2024-09-30 04:41:02 发布

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

我正在努力学习如何使用对分搜索来提高我编写的程序的速度。 但事实证明我失败得很惨。我已经被困了3个小时了,一直在检查我的代码,尝试不同的东西,谷歌搜索,阅读,但仍然一无所获。 所以我来到这里,希望这里的人能帮助我理解为什么我的代码会被破坏,以及我应该如何考虑不再处于这种情况。你知道吗

我尽可能使用print语句来查看我的输出。我看到的是,它只是重复打印相同的数字(猜测),而没有改变它。我想可能是因为一些变量的作用域不在范围内(不能改变?)那可能是我的问题?你知道吗

代码如下:

def evaluatePayment(balance, guess, annualInterestRate):
    annualInterestRate = annualInterestRate
    balance = balance
    minPay = guess
    for month in range(1,13):
        remainingBalance = balance - minPay
        balance = (remainingBalance + remainingBalance * annualInterestRate / 12.0)
        #print "month: " + str(month) + " balance: " + str(balance) + " minPay: " + str(minPay)
    if balance <= 0:
        return balance
    else:
        return balance


annualInterestRate = 0.2        
balance = 320000
monthInt = annualInterestRate / 12.0
upper= evaluatePayment(balance, 0, annualInterestRate)/12
lower = balance / 12.0
guess = (lower+upper)/2
while True:

    if (evaluatePayment(balance, guess, annualInterestRate)) <= 0.00 and (evaluatePayment(balance, guess, annualInterestRate)) > -0.13:
        print "Lowest Payment: " + str(guess) + " balance: " + str((evaluatePayment(balance, guess, annualInterestRate)))
        break
    elif (evaluatePayment(balance, guess, annualInterestRate)) > 0.00:
        upper = guess
        guess = (lower+upper)/2
        print str(guess)
    elif (evaluatePayment(balance, guess, annualInterestRate)) < 0.00:
        lower = guess
        guess = (lower+upper)/2
        print str(guess)

编辑: 固定压痕。看起来和我的档案不一样。我没有正确粘贴代码。你知道吗


Tags: 代码returnifupperlowerprintbalanceguess
1条回答
网友
1楼 · 发布于 2024-09-30 04:41:02

我在@Prerak Sola的帮助下解决了这个问题,并通过纸上的复习解决了这个问题。试着用手解决一次,一切都有意义:D

下面是更新后的代码:

"""
This function evaluates the guess to see if it'll solve the problem.
It returns the balance so that you can use it.
"""
def evaluatePayment(balance, guess, annualInterestRate):
    annualInterestRate = annualInterestRate
    balance = balance
    minPay = guess
    for month in range(1,13):
        remainingBalance = balance - minPay
        balance = (remainingBalance + remainingBalance * annualInterestRate / 12.0)
        #print "month: " + str(month) + " balance: " + str(balance) + " minPay: " + str(minPay)
    if balance <= 0:
        return balance
    else:
        return balance

#Defines the variables needed for this to work
annualInterestRate = 0.18       
balance = 999999
monthInt = annualInterestRate / 12.0
upper= evaluatePayment(balance, 0, annualInterestRate)/12
lower = balance / 12.0
guess = (lower+upper)/2

"""
This is where the "guessing" takes part. It will iterate untill it finds a solution.
It takes the original guess and tries it, if it returns false it will check if the
guess was to high or to low and try assign new values depending on the elif-statement.
"""
while True:
    if (evaluatePayment(balance, guess, annualInterestRate)) <= 0.00 and (evaluatePayment(balance, guess, annualInterestRate)) > -0.01:
        print "Lowest Payment: " + str(round(guess,2)) + " balance: "
        break
    elif (evaluatePayment(balance, guess, annualInterestRate)) > 0.00:
        lower = guess
        upper = upper
        guess = (lower+upper)/2
        #print "high " + str(guess)

    elif (evaluatePayment(balance, guess, annualInterestRate)) < 0.00:
        upper = guess
        lower = lower
        guess = (lower+upper)/2
        #print "low " + str(guess)

相关问题 更多 >

    热门问题