Question

In: Computer Science

The formula for calculating the amount of money in a savings account that begins with an...

The formula for calculating the amount of money in a savings account that begins with an initial principal value (P) and earns annual interest (i) for (n) years is: P(1 + i)n
Note that i is a decimal, not a percent interest rate: .1 NOT 10%

             Write a python program that does the following:

  • Prompt the user on three lines for principal, percent interest rate and number of years to invest (using descriptive prompts)
    • Use a while loop to keep prompting until entries are valid
  • Call your function calc_compound_interest() that:
    • takes the arguments principle, int_rate, years
    • uses the above formula to calculate the ending value of the account
    • returns the value
  • Call a second function calc_compound_interest_recursive() that:
    • takes the arguments principle, int_rate, years
    • calculates the value recursively – calling a base calculation over and over instead of using the number of year as an exponent.
    • return that value
  • Print both values with clear descriptions and formatted with thousand commas and 2 decimal places. Then print whether the two values are equal when rounded to 4 decimal places.


Solutions

Expert Solution

Code:

import math


def calc_compound_interest():
    ''' Asks user to input principle, interest and years to invest
        and calculates the end value using power function of math library.
    '''
    principle = float()  # initialize principle
    int_rate = float()  # initialize int_rate
    years = int()  # initialize years
    while type(principle) != 'float' or type(int_rate) != 'float' or type(
            years) != 'int':  # Check if the user inputs are of the right type
        try:
            principle = float(input('Enter the principle amount: '))  # Prompts user for principle
            int_rate = float(input('Enter the rate of interest: '))  # Prompts user for rate of interest
            years = int(input('Enter the number of years to invest: '))  # Prompts user for years
            break;
        except:
            continue

    end_val = principle * pow((1 + int_rate), years)  # Calculate the end value

    return end_val


def calc_compound_interest_recursive():
    principle = float()  # initialize principle
    int_rate = float()  # initialize int_rate
    years = int()  # initialize years
    while type(principle) != 'float' or type(int_rate) != 'float' or type(
            years) != 'int':  # Check if the user inputs are of the right type
        try:
            principle = float(input('Enter the principle amount: '))  # Prompts user for principle
            int_rate = float(input('Enter the rate of interest: '))  # Prompts user for rate of interest
            years = int(input('Enter the number of years to invest: '))  # Prompts user for years
            break;
        except:
            continue

    # Calculates the end value
    for y in range(years):
        if y == 0:
            end_val = principle * (1 + int_rate)
        else:
            end_val = end_val * (1 + int_rate)

    return end_val


def main():
    pow_val = round(calc_compound_interest(), 2)  # Round upto 2 decimals
    base_val = round(calc_compound_interest_recursive(), 2)  # Round upto 2 decimals
    print(f'End Value using power function= {"{:,}".format(pow_val)}')  # Print using commas
    print(f'End Value without using power function= {"{:,}".format(base_val)}')  # Print using commas


main()

Output:

Keep asking for inputs until vaild input is entered:


Related Solutions

Determine the amount of money in a savings account at the end of 8 years, given...
Determine the amount of money in a savings account at the end of 8 years, given an initial deposit of $40,000 and a 4 percent annual interest rate when interest is compounded: 1. Annually    2. Semiannually    3. Quarterly ​
22. Determine the amount of money in a savings account at the end of 3 years,...
22. Determine the amount of money in a savings account at the end of 3 years, given an initial deposit of $4,000 and an annual interest rate of 4 percent when interest is compounded: Use Appendix A for an approximate answer, but calculate your final answer using the formula and financial calculator methods. (Do not round intermediate calculations. Round your final answers to 2 decimal places.) Future value a. Annually b. Semiannually c. Quarterly 30. Your parents have accumulated a...
Suppose you have a certain amount of money in a savings account that earns compound monthly...
Suppose you have a certain amount of money in a savings account that earns compound monthly interest, and you want to calculate the amount that you will have after a specific number of months. The formula is as follows: f = p * (1 + i)^t • f is the future value of the account after the specified time period. • p is the present value of the account. • i is the monthly interest rate. • t is the...
1) Please explain the following terms: savings account, basic savings account, interest bearing checking account, money...
1) Please explain the following terms: savings account, basic savings account, interest bearing checking account, money market deposit accounts, and certificate of deposits 2) What is the difference between a bond and a certificate deposit
What is the formula to calculate how much a savings account would be worth if the...
What is the formula to calculate how much a savings account would be worth if the initial balance is $1,000 with monthly deposits of $75 for 10 years at 4.3% annual interest compounded monthly? What is the formula result? You want a savings account to grow from $1,000 to $5,000 within two years. Assume the bank provides a 3.2% annual interest rate compounded monthly. What is the formula to calculate how much you must deposit each month to meet your...
An amount of $10,000 is deposited into a savings account that pays interest at a rate...
An amount of $10,000 is deposited into a savings account that pays interest at a rate of 7%. If 10 equal annual withdrawals are made from the account starting one year after the money was deposited, how much can be withdrawn so that in the fifth year one would be able to withdraw an additional $1,000 and the account would be depleted after 10 years? Explain verbally in detail and sketch a timeline to illustrate.
You have just received an endowment and placed this money in a savings account at an...
You have just received an endowment and placed this money in a savings account at an annual rate of 13.58 percent. You are going to withdraw the following cash flows for the next five years. End of year 1.   $7,512 2.   $8,140 3.   $4,455 4.   $3,122 5.   $1,857 How much is the endowment that you received? Round the answer to two decimal places.
The amount of $5000 is placed in savings account where interest is compounded continuously at the...
The amount of $5000 is placed in savings account where interest is compounded continuously at the rate of 6% per year. How long will it take for this amount to triple?
In the USA, a Flexible Savings Account (FSA) plan allows you to put money into an...
In the USA, a Flexible Savings Account (FSA) plan allows you to put money into an account at the beginning of the calendar year that can be used for medical expenses. This amount is not subject to federal tax. As you pay medical expenses during the year, you are reimbursed by the administrator of the FSA until the money is exhausted. From that point on, you must pay your medical expenses out of your own pocket. On the other hand,...
You have invested money in a savings account that pays a fixed monthly interest on the...
You have invested money in a savings account that pays a fixed monthly interest on the account balance. The following table shows the account balance over the first 5 months. Time in months Savings balance 0 $1500.00 1 $1521.00 2 $1542.29 3 $1563.88 4 $1585.77 5 $1607.97 (a) How much money was originally invested? $   (b) Show that the data are exponential. (Round your answer to three decimal places.) Each successive ratio of new/old is   , which shows that the data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT