我正在尝试学习如何使用二分搜索来提高我编写的程序的速度。 但事实证明我失败得很惨。我已经被困了 3 个小时,检查我的代码,尝试不同的东西,谷歌搜索,阅读,但仍然一无所获。 所以我来到这里,希望这里有人能帮助我理解为什么我的代码会被破坏,以及我应该如何考虑不再遇到这种情况。
我正在尽可能使用打印语句来查看我的输出。我看到的是它只是重复打印相同的数字(猜测)而不改变它。我在想也许是因为某些变量范围不在范围内(无法更改?),这可能是我的问题?
代码如下:
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)
编辑: 固定缩进。它看起来与我的文件中的不一样。我没有以正确的方式粘贴代码。
最佳答案
我在@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)
关于python - 我的代码中的无限循环,我不明白它来自哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28025839/