Python 以外币存款的复利
我正在为一个comp sci班做作业。我觉得我真的很接近,但我不能完全得到答案。基本上,这个任务是一个复利计算器,我想做的是在初始投资中增加存款,允许某人在某一点停止支付,但在另一点收集。例如,“用户可能已经有Python 以外币存款的复利,python,financial,Python,Financial,我正在为一个comp sci班做作业。我觉得我真的很接近,但我不能完全得到答案。基本上,这个任务是一个复利计算器,我想做的是在初始投资中增加存款,允许某人在某一点停止支付,但在另一点收集。例如,“用户可能已经有 当他们开始计算退休金时,在他们的账户上存了10000美元。他们打算存钱 在接下来的10年里,每年再增加1000美元,届时他们将停止额外的收入 存入他们的帐户。但是,他们可能离退休还有20年。你的计划 应能够考虑这些不同的输入,并计算其正确的未来值 “退休账户” 以下是我目前的代码: de
当他们开始计算退休金时,在他们的账户上存了10000美元。他们打算存钱
在接下来的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()
我认为您需要确保公式正确: 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
测试:
收益率:
6720.0
10803.968
15926.8974592
到目前为止,主要工作已经完成,我想你可以处理其余的工作。在大多数情况下,我更喜欢雷的解析解——将值插入公式,得到最终答案,而不是逐年迭代 但是,在本例中,您需要每年的值,因此您不妨迭代:
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()
我有一个类似于上面提到的问题,但我不明白为什么方程(公式)的第二部分在第二个等号之后?
还有没有其他更简洁的方法,而不必多次对该部分进行编码“FV(t)=5000*(1.12**t)+1000*(1.12**t)+1000*(1.12**(t-1))
6720.0
10803.968
15926.8974592
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
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