-2

I'm just starting to learn about recursion for an EdX course, and I've written an iterative function to calculate the remaining balance after paying the minimum required payment for 12 months.

I was able to easily do it with iteration, but I can't seem to wrap my head around the recursive way.

Please point me in the right direction.

Here is my iterative function

def remaining_balance_iter(balance,annualInterestRate, monthlyPaymentRate ):
  '''
  This code will take any balance and annual interest rate and calculate the 
  balance after one year of making the minimum payments
  '''

  month = 1
  monthly_interest_rate = annualInterestRate/12.0

  while month <= 12:

    minimum_monthly_payment = monthlyPaymentRate * balance

    monthly_unpaid_balance = balance - minimum_monthly_payment

    balance = monthly_unpaid_balance + monthly_interest_rate*monthly_unpaid_balance

    print( "Month {} Remaining balance: ".format(month) + str(round(balance,2)))

    month += 1

  print ("Remaining balance " + str(round(balance,2)))

I've made an attempt at a recursive function, but it needs work, and I need tutoring haha

def remaining_balance_recur(balance,annualInterestRate, monthlyPaymentRate, month ):
  '''
  This code will take any balance and annual interest rate and calculate the 
  balance after one year of making the minimum payments
  '''

  month = 1
  monthly_interest_rate = annualInterestRate/12.0

  while month <= 12:

    minimum_monthly_payment = monthlyPaymentRate * balance

    monthly_unpaid_balance = balance - minimum_monthly_payment

    interest = monthly_interest_rate*monthly_unpaid_balance

    balance = remaining_balance_recur(monthly_unpaid_balance, annualInterestRate, monthlyPaymentRate, month + 1) + interest

  print ("Remaining balance " + str(round(balance,2)))
Matt
  • 782
  • 4
  • 11

2 Answers2

0

The best way I've found to deal with recursion is to start by specifying a base case. What is the condition that tells you when you've finished your method? In your code, it looks like you run your method until `month > 12', so your base case would be:

if month > 12:
    return 1 # 1 for the purpose of explanation

Your return value for your base case is some base value of your function. What would your script return if your month was 12? That's the value you would return.

Next is the hard part. You have to figure out what variable is being modified by subsequent calls to your method. I'm not exactly sure what your code is intended to do, but it looks like you have a few calculations on some variables. When you use recursion, it's almost as if you're saving the state of the current method call you are executing while you go and retrieve the value you need for your statement. (e.g. a_num = 1 + recurse(n - 1) - you need the value of recurse(n - 1) before you can continue with this statement. This is only an example, though). Look for the variable that is affected by your previous iterations and try to make that recursive. In your situation, it looks like balance is that variable:

balance = balance + remaining_balance_recur(annualInterestRate, monthlyPaymentRate, month + 1)
return balance

When you write a recursive method, you always need to return some value at the end of the method, so the statement that called the method actually gets a value. Here's a short, useless example:

def recurse(n)
    if n == 0       # BASE CASE
        return 1
    some_sum = 0
    some_sum += recurse(n - 1)    # I need the value from recurse(n - 1)

    return some_sum    # This method was called somewhere, so it needs to return

Try to figure out a recursive solution for your code from these hints. I'm sorry, recursion is very hard to explain especially over SO. Youtube vids and Google would also be a big help to understand recursion in general. Hope this gave you some ideas.

Peter
  • 323
  • 1
  • 6
0

By putting "month = 1" before the while statement, you are resetting it so that while month <= 12 will run forever. This creates a "RecursionError: maximum recursion depth exceeded in comparison."

The output of your function is currently "NoneType" because its output is a print statement rather than returning an int or a float. This produces a "TypeError" because you have tried to add a float (the interest rate) to a NoneType (the printed statement) in your recursion.

Chase G.
  • 113
  • 5