我遇到了一个来自在线课程(Python入门)的问题。基本上,我们被告知要使用二分法搜索来查找每月最低的固定支付额,以在给定余额和年利率时在一年内清除债务(四舍五入到最接近的0.01美元)。我的解决方案在上传到他们的在线平地机时,仅给出了此错误:Python中的二进制搜索无限循环?
“运行您的解决方案时出现问题。 我们无法运行您的解决方案。
我可能在一个无限循环?如果是这样,我不太明白如何。原始代码在下面发布。谢谢大家花时间阅读本文!
MonthlyInterestRate = annualInterestRate/12
month = 1
LB = balance/12
UB = balance*(2.7/12)
check = balance
while abs(balance) > 10:
payment = (LB + UB)/2
while month <= 12:
balance = (balance - payment)*(1 + MonthlyInterestRate)
month = month + 1
if balance > 10:
LB = payment
balance = check
elif balance < -10:
UB = payment
balance = check
else:
print('Lowest Payment: ' + str(payment))
break
这是完整的代码吗?您还没有在任何地方定义'annualInterestRate'或'balance'。 –
服务器不会给你完整的回溯?当您在计算机上本地运行时会遇到什么错误? –
1.在线平地机上尝试“hello world”示例,以确保正确处理输入/输出。 2.在你的机器上本地测试你的解决方案。 3.将利率逻辑封装在单独的对象中(分别测试二分查找和余额计算,例如,如果有[复合](http://en.wikipedia.org/),'MonthlyInterestRate = annualInterestRate/12'是错误的/ wiki/Compound_interest))4.使用整数或decimal.Decimal进行货币算术运算。 – jfs