Continuing on with my retirement simulation here is problem two.

Write a function, called nestEggVariable, which takes three arguments: a salary

(salary), a percentage of your salary to save (save), and a list of annual growth

percentages on investments (growthRates). The length of the last argument defines the

number of years you plan to work; growthRates[0] is the growth rate of the first year,

growthRates[1] is the growth rate of the second year, etc.  (Note that because the

retirement fund’s initial value is 0, growthRates[0] is, in fact, irrelevant.)  This function

should return a list, whose values are the size of your retirement account at the end of

each year.

Without further ado, here is my function:

def nestEggVariable(salary,save,growthRates):
base = salary*save*.01
savingsRecord = [base,]
for growthRate in growthRates[1:]:
accountvalue = base + (1 + 0.01 * growthRate)*savingsRecord[-1]
return savingsRecord
First off, the main difference between this function and the previous function is that we’re interacting with variable interest rates.  That means that the variable ‘growthRates’ is actually a list, with each entry being a different interest rate percentage.  Because I have a list as a variable, I need to refine and adjust my function a bit.

I begin, as before, by creating my base year definition, defining the base year earnings.  Then I create a new list ‘savingsRecord’ with the first entry being my base year earnings.  This time, instead of using a while loop I use a for loop.  This way I can process through each entry in the list variable that was inputted into the function.  Therefore for each ‘growthRate’ within the ‘growthRates’ list I am to process the following: define an ‘accountvalue’ for each year and append each new ‘accountvalue’ to the ‘savingsRecord’ list.  Once all items in the ‘growthRates’ list have been processed I am to return the list.  When using the following variables I come up with these results:


print nestEggVariable(10000,10,[3,4,5,0,3])


[1000.0, 2040.0, 3142.0, 4142.0, 5266.26]