Question: - In the code editor, write a function named compound that takes three inputs: balance, rate, and num _ periods. That function should take the
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
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.
Next
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
