0

I am trying to make a program that calculates compound interest with 3 different lists. The first item in each list are the variables needed in the formula A=P(1+r)^n. These are the instructions.

Albert Einstein once said “compound interest” is man’s greatest invention. Use the equation A=P(1+r) n
,
where P is the amount invested, r is the annual percentage rate (as a decimal 5.0%=0.050) and n is the
number of years of the investment.
Input: 3 lists representing investments, rates, and terms
investment = [10000.00, 10000.00, 10000.00, 10000.00, 1.00]
rate = [5.0, 5.0, 10.0, 10.0, 50.00]
term = [20, 40, 20, 40, 40]
Output: Show the final investment.
$26532.98
$70399.89
$67275.00
$452592.56
$11057332.32

This is the code that I have written so far:

P = [10000.00, 10000.00, 10000.00, 10000.00, 1.00]
r = [5.0, 5.0, 10.0, 10.0, 50.00]
n = [20, 40, 20, 40, 40]

# A=P(1+r)
def formula(principal,rate,years):
    body = principal*pow((1 + rate),years)
    print "%.2f" %(body)
def sort(lst):
    spot = 0
    for item in lst:
        item /= 100
        lst[spot] = item
        spot += 1

input = map(list,zip(P,r,n))
sort(r)
for i in input:
    for j in i:
        formula()

I firstly define a function to calculate the compound interest, then I define a function to convert the rates to the proper format. Then using map (which im not completely familiar with)I separate the first item of each list into a tuple within the new input list. What i am trying to do is find a way to input the three items within in the tuple into principle, rate and years in the formula function. I am open to critiquing, and advice. I am still fairly new with programming in general. Thank you.

1
  • 2
    Judging by the title I have a feeling that your actual question could and should be decoupled from your specific code, with a clear, concise MCVE and without all the boilerplate. Commented Mar 7, 2016 at 17:22

1 Answer 1

1

Firstly, I think you should return something from your formula, that is, your calculation:

def formula(principal,rate,years):
    return principal*pow((1 + rate),years) #return this result

then you can use the return value from the formula - be it for printing or any other use for further calculation.

Also, since you have the same amount of items in your three lists, why not just using range(len(p)) to iterate over them?

for x in range(len(p)):
    print(formula(p[x],r[x],n[x]))

x in range(len(p)) will generate iteration with x value:

0, 1, ..., len(p) - 1 # in your case, len(p) = 5, thus x ranges from 0 to 4

And p[x] is the way you say you want to get x-th-indexed element from p. Putting it you your context you will get combination like this:

when x=   principal   rate   years
----------------------------------
  0       10000.00     5.0     20
  1       10000.00     5.0     40
  2       10000.00    10.0     20
  3       10000.00    10.0     40
  4           1.00    50.0     40

This way, you do not need to use tuple

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks so much for the feedback. It works! :) On another note, how can i format the outputs to round to the hundredths place if I'm using return? I used round that worked, but when the result was 0 even it only displayed one zero. I'm really OCD about that stuff so I'm trying to use the %.2f somehow. So the answer is 67284.0, and i want it to look like 67284.00
@avbirm check this recent post. Seems like what you want. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.