最小支付程序超过最大递归深度

2024-04-27 01:05:19 发布

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

下面的错误发生表明基本情况是没有达到,由我不知道为什么是这样的情况。我使用二分法搜索债务支付问题:

我想找出一年内可以付清的最低付款额(-0.1<;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)

Tags: andthetoinpaymentupperlowercalculate