发生以下错误表示未达到基本情况,我不知道为什么会这样。我正在使用二分法搜索债务支付问题:
我想找出最小的付款,以便可以在一年内还清未付款(-0.1&lt; 12个月后未兑现<0)
File "problem3.py", line 21, in calculate
calculate(balance, annualInt, min, lower, upper)
File "problem3.py", line 5, in calculate
while month < 12 and outstanding > 0:
RecursionError: maximum recursion depth exceeded in comparison
我的代码:
#find the smallest monthly payment such that we can pay off the debt within a
#year
def calculate(balance, annualInt, minpay, lower, upper):
outstanding = balance
monInt = annualInt/ 12.0
month = 0
while month < 12 and outstanding > 0:
outstanding = round((outstanding*(1+monInt) - minpay),2)
month += 1
if outstanding < 0 and outstanding > -0.1: # set outstanding range (-0.1 < x < 0)
print('Montly payment to pay off debt in 1 year:', round(minpay,2))
print('Number of months needed:', month)
print('Balance:', round((outstanding),2))
elif outstanding <= -0.1:
upper = minpay
minpay = round(((lower + upper) /2.0), 2)
calculate(balance, annualInt, minpay, lower, upper)
else:
lower = minpay
minpay = round(((lower + upper)/ 2.0), 2)
calculate(balance, annualInt, minpay, lower, upper)
balance = round(float(input('Enter the outstanding balance on your credit card:')),2)
annualInt = round(float(input('Enter the annual credit card interest rate as a decimal')),2)
# set lower to Monthly payment lower bound
lower = balance / 12.0
# set lowet to Monthly payment upper bound
upper = (balance*(1+(annualInt/12.0))**12.0)/12.0
# set minpay to the smallest monthly payment
minpay = round(((lower + upper)/2.0), 2)
calculate(balance, annualInt, minpay, lower, upper)