Click here to Skip to main content
15,889,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to develop a program for compound interest allowing monthly contributions in Python. Syntax aside- need help with formula.

What I have tried:

Python
Init_Amt = input("Enter initial amount")
Interest_rate = input("Enter the interst rate")
Num_years = input("Number of years")
Monthly_Amount = input("Monthly Amount")

Init_Amt = float(Init_Amt)
Interest_rate = float(Interest_rate)
Num_years = int(Num_years)
Monthly_Amount = float(Monthly_Amount)

PV = Init_Amt
IR = Interest_rate
N = Num_years


N_m = N*12 #in terms of months
M = Monthly_Amount



def compound(PV, N, M, IR):
    for i in range(N):
        PV += (M * 12)
        FV = (PV*(1+IR)**N)
        print(i + FV)




if __name__ == "__main__":
    compound(PV, N, M, IR)
Posted
Updated 27-Nov-16 20:50pm
v5
Comments
Peter Leow 27-Nov-16 21:47pm    
I see that you have followed solution 1 to add indentation, but still not right, you have not understand the working of indentation in Python, see added answer in solution 1. Do not down vote anyhow just because that solution did not work due to your poor understanding.

1 solution

You never state the problem you have encountered. Formula aside, I will only comment on your code which is the purpose of this forum. Suggest you revisit the basic syntax of Python which is the indentation of code block, you failed to observe this in:
def compound(PV, N, M, IR):

and
if __name__ == "__main__":

Read Lexical analysis — Python 3.5.2 documentation[^] and figure it out yourself.

+++++[Round 2]+++++
I saw that you have added the indentation, but you still miss the point, I will provide you the answer below:
def compound(PV, N, M, IR):
    for i in range(N):
        PV += (M * 12)
        FV = (PV*(1+IR)**N)
        print(i + FV)

if __name__ == "__main__":
    compound(PV, N, M, IR)

The indentation is Python' way of grouping related code as a block, must like the {} used in other programming languages.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900