复杂的兴趣与Python存款

时间:2014-02-04 04:33:34

标签: python financial

我正在为comp sci课程做作业。我觉得我很亲密,但我不能得到答案。基本上,这项任务是一个复利计算器,我试图做的更复杂的是在初始投资中增加存款并允许某人在某一点停止支付,但在不同的点收集它。例如“,用户可能已经有了 他们开始退休计算时,在账户中节省了10,000美元。他们打算保存 未来10年每年另外1000美元,此时他们将停止任何额外的工作 存入他们的帐户。然而,他们可能距离退休还有20年。你的计划 应该能够解释这些不同的输入并计算其正确的未来价值 退休账户“

到目前为止,这是我的代码:

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()

我希望输出看起来像这样:

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!

3 个答案:

答案 0 :(得分:2)

我认为你需要确保公式是正确的:

  

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

然后我们可以定义一个函数:

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

测试:

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

收率:

6720.0
10803.968
15926.8974592

到目前为止主要工作已经完成,我认为你可以处理剩下的工作。

答案 1 :(得分:0)

在大多数情况下,我更喜欢Ray的分析解决方案 - 将值插入公式,得到最终答案,而不是逐年迭代。

但是,在这种情况下,你想要每年的值,所以你也可以迭代:

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()

导致

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

答案 2 :(得分:0)

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

我有类似上面提到的问题,但我不明白为什么第二个等号后的等式(公式)的第二部分? 也没有其他方法可以更简洁,无需编码&#34; FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1))&#34;这部分好几次?