Basics
While you can loop through every year to determine the ending value, there is also a simple formula which can calculate at each point in time what the value of the account should be. This formula is:
$ A = P * (1+r)^t $
where
$ A = \text{Ending value of the account} $
$ P = \text{Starting principal of the account} $
$ r = \text{Annual rate of return} $
$ t = \text{Number of years} $
In [4]:
p = 100
r = .05
for t in range(1,6):
A = p * (1+r) ** t
print("----Year {}----".format(t))
print("Ending Principal: ${}".format(A))
print("---------------")
print()
print()
To make our life easy, let's create a function that will compute the future value of an investment and then check to make sure it works.
In [5]:
def compoundInterest(p,r,t):
A = p*(1+r)**t
return A
p = 100
r = .05
for t in range(1,6):
A = compoundInterest(p,r,t)
print("----Year {}----".format(t))
print("Ending Principal: ${}".format(A))
print("---------------")
print()
print()