F01.justanswer.com



Page 1 of 2InstructionsExamine the starter code in the editor window, and run that code in the 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 let’s 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.) You’ll 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 you’d 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 let’s 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 don’t 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.With the answers to the previous three questions, write a Python statement that will compute the number of grains of wheat on each square of the chessboard, using the?compound_by_period?function you have written. Store the result in a variable named?wheat, and print its value to the screen to marvel at how much wheat there is.The list named?wheat?that you produced reflects the number of grains on each square. Write an expression to compute the total number of grains on the board, using the list?wheat?and one of the built-in functions that you’ve already learned about, and store it in a variable named?total_wheat.Wheat starting balance = 1, rate 100%, periods =63 Current code: Balance = 100rate = 0.03num_periods = 10print(0, round(balance,2))for n in range(1,11): balance = round(balance * (1 + rate), 2) print(n, round(balance,2))def compound_interest(balance,rate,num_periods): compound=balance*(pow((1+rate/100),num_periods)) print(compound)compound_interest(100,3,10)Second part: In the code editor, add the following docstring to the function?add_two_strings?and inspect the docstring in the interpreter:"""Returns the concatenation of the first string and the second string, separated by the separator (default=' ').""" ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches