Answered step by step
Verified Expert Solution
Link Copied!

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 $100, and which grows over time at a rate of 3% per year. The balance at the end of a given year is equal to balance*rate from the year before. The current balance is rounded to the nearest two decimal places (i.e., $0.01), and is printed out each year for a total period of 10 years. (The balance at year 0 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 num_periods. 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 num_periods 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 (i.e., 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=100, rate=0.03, num_periods=10. 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 compound_by_period 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 compound_by_period with the inputs as balance=100, rate=0.03, num_periods=10. 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 change_per_period 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 1 and year 0 balances, the second element will contain the difference between year 2 and year 1, 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

3. Define the roles individuals play in a group

Answered: 1 week ago