The next problem in this set uses the same pattern from the previous functions, but in a way you’re reversing the process.  This time you’re to create a function that will take the total amount you saved for your retirement, and determine how much will be left based on your spending over each year also including the different growth rates for each year.  Here is the problem in its full definition:

Write a function, called postRetirement, which takes three arguments: an initial amount

of money in your retirement fund (savings), a list of annual growth percentages on

investments while you are retired (growthRates), and your annual expenses (expenses).

Assume that the increase in the investment account savings is calculated before

subtracting the annual expenditures (as shown in the above table).  Your function should

return a list of fund sizes after each year of retirement, accounting for annual expenses

and the growth of the retirement fund. Like problem 2, the length of the growthRates

argument defines the number of years you plan to be retired.

Note that if the retirement fund balance becomes negative, expenditures should continue

to be subtracted, and the growth rate comes to represent the interest rate on the debt

(i.e. the formulas in the above table still apply).

Here is my end function:


def postRetirement(savings,growthRates,expenses):
remainingSavings = [savings,]
for growthRate in growthRates:
remainingvalue = remainingSavings[-1]*(1 + 0.01 * growthRate) - expenses
return remainingSavings[1:]


As you can see, as like the two previous functions, I begin with defining a new list ‘remainingSavings’, with my savings as the first entry.  Then I use a ‘for’ loop to process the ‘growthRates’ list I was provided.  First step in the ‘for’ loop, assess the ‘remainingvalue’ for the next year.  This is done by taking the last entry from the ‘remainingSavings’ list (which is denoted by the slice of [-1] previous the list name), and multiplying it by the total growth then subtracting the estimated expenses for each year.  The final part is to append each ‘remainingvalue’ to the ‘remainingSavings’ list, and then going through the loop again.  After the loop is finished it then returns a slice of that list (all of it excepting the first position), and it’s done.  Here are the variables and the subsequent result from my function.


print postRetirement(100000,[10,5,0,5,1],30000)

[80000.000000000015, 54000.000000000015, 24000.000000000015,
   # -4799.9999999999854, -34847.999999999985]