简体   繁体   中英

My code throws recursive error. Can somebody explain to me what's wrong with my base code in recursion?

I'm finding out the minimum payment to make, to ensure the debt is fully settled after 12 months

The unpaid credit balance is compounded every month by (a/12)%

I just started learning recursion and couldn't figure out why my base code for recursion is wrong. The console throws up recursion error. Here's part of the code.

#minPayment (3329,0.2)

def minPayment (cb, a):

'''
cb = initial credit balance
a = annual interest (in decimals)
'''

  x = 0
  unpaid = cb-x
  inc = (cb-x)*(a/12)

  def mintha (x):
     totalPay = unpaid + inc

     if (12*x - totalPay) >= 0:
         return x

     else:
         totalPay = unpaid + inc
         return mintha (x+10) 


 return mintha (x)

Expected result = 310
Actual result = 290

A few problems with the code:

  • The function mintha is called again in the return statement of the function, return mintha(x) which can cause an endless loop
  • In the return statement for the else clause: return totalPay and mintha (x+10) you are calling the function again. This can lead to recursion problems if the maximum depth is reached.

So, we correct the code and assign some hypothetical values for x , cb and a and give this a go. While returning the values, we round them to the nearest integer.

x = 0
cb = 25000
a = 50000
unpaid = cb-x   # unpaid balance
inc = unpaid*(a/12)  # increment 

def mintha (x):

    '''
    x is the minimum payable per month to pay off debt (round to tenth) 
    '''

    totalPay = unpaid + inc      # total credit balance, including the 
                                #    interests

    if (12*x - totalPay) >= 0:
        return round(x)

    else:
        totalPay = unpaid + inc
        return round(totalPay)

print(mintha(3000))
#Output:
104191667

PS: I'm not entirely sure about the math behind this problem.

It's a little hard to see what's happening without knowing what values all the variables could have, but I can say this:

  1. It looks like the bottommost return mintha(x) won't ever be reached, since each clause in the if/else above it will return something.

  2. The else clause, which ends return totalPay and mintha (x+10) has slightly unintuitive behavior. As @MichaelVeksler pointed out above, this will return mintha(x+10) if totalPay is false (ie, 0) and otherwise it will return totalPay .

So, function return if/else logic you have written is something like this:

if (12*x - totalPay) >= 0:
    return x
else:
    if totalPay == 0:
        return totalPay
    else:
        return mintha (x+10) # recurse

I would closely examine the condition if (12*x - totalPay) >= 0: and make sure it will eventually evaluate to true, as that would break you out of the infinite recursion. (It seems like it may never become true since totalPay is increased [maybe?] with every totalPay = unpaid + inc statement.)

The other way to break out of the infinite recursion is if totalPay is 0, but again I'm not sure this is what was intended.

Note: This answer does not speak to issues with the code producing a meaningful answer, it just tries to explain why infinite recursion is happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM