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.
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