Adding Compound Frequency Option

Hello,

 Could someone help me with this program? I need to alter it and add the compound frequency option. Any help would be greatly appreciated. 

Accept the inputs

startBalance = float(input("Enter the investment amount: "))

years = int(input("Enter the number of years: "))

rate = int(input("Enter the rate as a %: "))

Convert the rate to a decimal number

rate = rate / 100

Initialize the accumulator for the interest

totalInterest = 0.0

Display the header for the table

print("%4s%18s%10s%16s" % \

("Year", "Starting balance",

"Interest", "Ending balance"))

Compute and display the results for each year

for year in range(1, years + 1):

interest = startBalance * rate

endBalance = startBalance + interest

print("%4d%18.2f%10.2f%16.2f" % \

(year, startBalance, interest, endBalance))

startBalance = endBalance

totalInterest += interest

Display the totals for the period

print(“Ending balance: $%0.2f” % endBalance)

print(“Total interest earned: $%0.2f” % totalInterest)

Hi @Tennyson5806,

It is difficult for us to copy, paste, and execute your code, since it is not properly formatted for posting. There are several options for formatting code for proper display. Here are three ways of doing it:

  1. Copy and paste the code into the window in which you are composing your post. Select all of the code, and click the </> button at the top of the editing window.

  2. Copy and paste the code into the window in which you are composing your post. Leave a blank line before and after the code. Add four spaces to the beginning of every line of code, including the lines that are already indented.

  3. Copy and paste the code into the window in which you are composing your post. Place three backticks on the line preceding your code and three backticks on the line that follows your code.

My apologies for posting it incorrectly. Thank you so much for showing me how to do it. Please see below.

# Accept the inputs
startBalance = float(input("Enter the investment amount: "))
years = int(input("Enter the number of years: "))
rate = int(input("Enter the rate as a %: "))

# Convert the rate to a decimal number
rate = rate / 100

# Initialize the accumulator for the interest
totalInterest = 0.0

# Display the header for the table
print("%4s%18s%10s%16s" % \
	("Year", "Starting balance",
	"Interest", "Ending balance"))

# Compute and display the results for each year
for year in range(1, years + 1):
	interest = startBalance * rate
	endBalance = startBalance + interest
	print("%4d%18.2f%10.2f%16.2f" % \
	(year, startBalance, interest, endBalance))
	startBalance = endBalance
	totalInterest += interest

# Display the totals for the period
print("Ending balance: $%0.2f" % endBalance)
print("Total interest earned: $%0.2f" % totalInterest) Implementation

Thanks for formatting the code.

As written, it appears that your program computes interest each year, which includes compounding it at the same time. Please explain what a compound frequency option would entail. Would this involve an option of computing and compounding the interest more than once per year?

Quercus,
I hope this provides more information, but these are my instructions: An alteration to your code should include: a compound frequency option *annually, semi-monthly, monthly, daily.

Thanks for the clarification. Have you learned about nested loops yet? There is more than one way to organize your solution, including an option of using a nested loop.

If compounding is performed more than once per year, are you expected to display the interest and balance each time the compounding is performed? In making this decision, take into account that displaying the account status on a daily or weekly basis would produce a large amount of output.

No, I haven’t heard of nested nested loops, but I will make sure to research the topic. Thank you.

A nested loop is a loop inside another loop. For this problem, you could place another loop header indented under this one:

for year in range(1, years + 1):

The new header would control an inner loop that would iterate a number of times corresponding to how many times per year compounding would occur. Of course, if compounding is performed multiple times per year, the amount of interest applied each time would be an appropriate fraction of the annual rate. For example, if compounding is performed each week, it would be based on 1/52 of the annual interest rate.

If you wish to try a nested loop, you can post your revised code when you have it, so we can help.

Hi S. Tennyson,

You can ask the user for a compounding frequency. One way is to accept
a number, any number at least 1.

(Frequency of 1 would be yearly or annually; frequency of 365 would be
daily; frequency of 12 would be monthly, frequency of 17 would mean 17
times per year which corresponds to approximately every three weeks…)

Then adjust your interest rate. Remember, interest rates are quoted as
per annum (per year), so if the frequency is 1 the interest rate doesn’t
need to change, if the frequency is 365 you divide by 365, if the
frequency is 12 you divide by 12, and so on.

Then loop:

for each year:
    for each period in the year:
        compute interest etc

I will allow you to translate that into proper Python code. (If you need
help, show us what you tried.)

The alternative approach is to ask for a compounding frequency, but
instead of accepting a number, you ask the user to type a letter or
other symbol for one of a small list of acceptable frequencies. Such as
Y for yearly, D for daily, M for monthly, and so on through the list.

Then once you have an acceptable letter (complaining to the user if they
type something unrecognised like “Q” or “Hello”), look up the
appropriate numeric frequency from a dict, and then proceed as above.

Obviously the first approach (a numeric frequency) is more flexible and
rather easier to program, but the second approach (only allow a handful
of permitted frequencies) is what you were literally asked to do.

If you wanted to be even cleverer, you could accept either a numeric
frequency or a letter code, so the user could enter either D or 365 to
get daily.