user3268414
user3268414

Reputation: 1

Compound interest with deposits in Python

I am working on an assignment for a comp sci class. I feel like I am really close but I cant quite get to the answer. Basically the assignment is a compound interest calculator, what I am trying to do that makes it more complicated is adding deposits to the initial investment and allowing for someone to stop paying into it at one point, but collect it at a different point. The example is ", a user may already have
saved $10,000 in their account when they start their retirement calculation. They intend to save
another $1000 per year for the next 10 years at which point they will stop making any additional
deposits into their account. However, they may be 20 years away from retirement. Your program should be able to account for these varying inputs and calculate the correct future value of their
account at retirement"

Here is my code so far:

def main():
    print("Welcome to Letmeretire.com's financial retirement calculator!")

    import random
    num = random.randrange(10000)

    principal = int(input("How much are you starting with for your retirement savings?"))
    deposit = int(input("How much money do you plan to deposit each year?"))
    interest = int(input("How much interest will your account accrue annually"))
    time = int(input("Please enter the number of years that you plan to deposit money for."))
    time_till_retirement = int(input("How long until you plan on retiring? (Please enter this amount in years)"))
    t = time + 1
    APR = interest/100
    R = ((1+APR/12)**12)-1
    DR = deposit/R
    DRT = deposit/(R*(1+R)**time)
    PV = principal+(DR-DRT)
    future_value = PV*((1+APR/12)**12*time)
    if time < time_till_retirement:
        time1 = (time_till_retirement-time)
        future = future_value*((1+APR/12)**12*time1)
    else:
        future = future_value
    for i in range(1, t):
        print("After " + str(i) + " years you will have "+ str(future) + " saved!")

main()

I would like the output to look like this:

Enter   annual  deposit:    1000    
Enter   interest    rate:   12  
Enter   number  of  years   until   retirement: 10  
What's  the current balance of  your    account:    5000    
How many    years   will    you make    your    annual  deposit?    5   

After   1   year,   you have:   $   6720.0  
After   2   years,  you have:   $   8646.4  
After   3   years,  you have:   $   10803.97    
After   4   years,  you have:   $   13220.44    
After   5   years,  you have:   $   15926.9 
After   6   years,  you have:   $   17838.13    
After   7   years,  you have:   $   19978.7 
After   8   years,  you have:   $   22376.14    
After   9   years,  you have:   $   25061.28    
After   10  years,  you have:   $   28068.64    


But what Im getting is this:

Welcome to Letmeretire.com's financial retirement calculator!

How much are you starting with for your retirement savings?5000

How much money do you plan to deposit each year?1000

How much interest will your account accrue annually12

Please enter the number of years that you plan to deposit money for.5

How long until you plan on retiring? (Please enter this amount in years)10


After 1 years you will have 271235.9643776919 saved!

After 2 years you will have 271235.9643776919 saved!

After 3 years you will have 271235.9643776919 saved!

After 4 years you will have 271235.9643776919 saved!

After 5 years you will have 271235.9643776919 saved!

Upvotes: 0

Views: 12966

Answers (3)

Naz
Naz

Reputation: 1

FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1)) + ... + 1000 * 1.12 = 5000 * (1.12 ** t) + 1000 * (1.12 ** t - 1) * 1.12 / 0.12

I have a similar problem like the one mentioned above but I do not get why the second part of the equation(formula) after the second equal sign? Also is there not another way of doing this more concise without having to code "FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1))" this part several times?

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56654

In most cases, I would prefer Ray's analytic solution - plug the values into a formula, get the final answer, instead of iterating year by year.

However, in this case, you want the values for each year, so you may as well iterate after all:

import sys

# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
    rng = xrange
    inp = raw_input
else:
    rng = range
    inp = input

def getter_fn(datatype):
    if datatype == str:
        return inp
    else:
        def fn(prompt=''):
            while True:
                try:
                    return datatype(inp(prompt))
                except ValueError:
                    pass
        return fn

get_float = getter_fn(float)
get_int   = getter_fn(int)

def main():
    print("Welcome to Letmeretire.com's financial retirement calculator!")

    principal  = get_float("Initial investment amount? ")
    periods    = get_int  ("How many years will you make an annual deposit? ")
    deposit    = get_float("Annual deposit amount? ")
    apr        = get_float("Annual interest rate (in percent)? ") / 100
    retirement = get_int  ("Years until retirement? ")

    deposits    = [deposit] * periods
    no_deposits = [0.] * (retirement - periods)

    amount = principal
    for yr, d in enumerate(deposits + no_deposits, 1):
        amount = (amount + d) * (1. + apr)
        print('After {:>2d} year{} you have:   $ {:>10.2f}'.format(yr, 's,' if yr > 1 else ', ', amount))

if __name__ == '__main__':
    main()

which results in

Welcome to the Letmeretire.com financial retirement calculator!
Initial investment amount? 5000
How many years will you make an annual deposit? 5
Annual deposit amount? 1000
Annual interest rate (in percent)? 12
Years until retirement? 10
After  1 year,  you have:   $    6720.00
After  2 years, you have:   $    8646.40
After  3 years, you have:   $   10803.97
After  4 years, you have:   $   13220.44
After  5 years, you have:   $   15926.90
After  6 years, you have:   $   17838.13
After  7 years, you have:   $   19978.70
After  8 years, you have:   $   22376.14
After  9 years, you have:   $   25061.28
After 10 years, you have:   $   28068.64

Upvotes: 0

Ray
Ray

Reputation: 2508

I think you need to ensure the formula is correct:

FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1)) + ... + 1000 * 1.12 = 5000 * (1.12 ** t) + 1000 * (1.12 ** t - 1) * 1.12 / 0.12

Then we can define a function:

def fv(t, initial, annual, interest_rate):
    return initial * (1+interest_rate) ** t + \
           annual * (1+interest_rate) * ((1+interest_rate) ** t - 1) / interest_rate

Test:

print fv(1, 5000, 1000, 0.12)
print fv(3, 5000, 1000, 0.12)
print fv(5, 5000, 1000, 0.12)

Yields:

6720.0
10803.968
15926.8974592

Till now the main work is done, I think you can handle the rest.

Upvotes: 3

Related Questions