Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Examine the starter code in the editor window, and run that code in the IPython interpreter. What this code is describing is the growth of
Examine the starter code in the editor window, and run that code in the IPython interpreter. What this code is describing is the growth of a pot of money, which started at $ and which grows over time at a rate of per year. The balance at the end of a given year is equal to balancerate from the year before. The current balance is rounded to the nearest two decimal places ie $ and is printed out each year for a total period of years. The balance at year is just the initial principal invested.
This is the sort of calculation that we would probably like to do repeatedly, so lets write a function to capture the basic logic.
In the code editor, write a function named compound that takes three inputs: balance, rate, and numperiods. That function should take the initial balance, a fixed interest rate, and the number of time periods over which the balance is to be compounded. If the interest rate represents a yearly interest rate, then numperiods would correspond to the number of years; if it is a monthly interest rate, it would reflect the number of months. Youll want your function to return the current balance ie the total of the principal plus all accrued interest at the end of the function so that you know how much money you have if you would like to reinvest it
Run the code file again in the interpreter, and test out your function with the inputs as balance rate numperiods Does it print out the same running balance as the starter code that ran originally?
Your function is useful, but maybe youd like to have a record of what the balance was at the end of each year, rather than just a print out of it on the screen. So lets create another function that takes care of that. Write a new function named compoundbyperiod that takes the same three inputs as before; instead of writing it from scratch, you can copy the previous function definition, paste a new function definition below, and then change the name of the function. In this new function, you dont want to just update and print the balance each year, but keep a list of what those yearly balances are that you can return at the end. Initialize an empty list at the start of the function, and then append the yearly balance to the end of the list each time through the loop. Instead of returning just the current balance at the end, return the entire list of yearly balances the last element of which will be the current balance
Run the code file again in the interpreter, and test out your new function compoundbyperiod with the inputs as balance rate numperiods Does it return a list with the same yearly balances that were previously printed to the screen?
Since we now have our yearly balances stored in a list, it is easy for us to write other functions to process that data. Write a new function named changeperperiod that takes a list of yearly balances and returns a new list that contains the change in account value from year to year. So the first element of this new list will contain the difference between the year and year balances, the second element will contain the difference between year and year etc. Since you are calculating the difference between two consecutive years, the list that is returned by this new function will have one element less than the list of yearly balances that you input.
Test out your new function with the list of yearly balances you computed previously, and print out the list of changes. Presumably each year the amount of increase should be a little bit larger, since the balance is growing over time this is the power of compound interest, although with such a small rate of growth, the increases are not so impressive.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started