0

我正在为人们的 401k 建立一个财务模型,该模型在方程中使用每年的随机回报率。我想知道在执行循环时如何让返回率从一个变为下一个。换句话说,我希望在每年计算蜡球时使用随机回报率。

例如,第 1 年 2000 * 1.10 = 2,200 第 2 年 2,100 * 1.2 = 2,520*

*这不包括我的代码中的wax_ball 和贡献变量。我的问题似乎与选择功能有关...我已将其导入以从 3 种不同的退货率中进行选择。

下面是我的代码。

谢谢你的帮助。XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

print "How much do you want to contribute to your 401k?"
contribution = int(raw_input())
# money contributed to 401k
print "What is your 401k worth now?"
wax_ball = int(raw_input())
# sum of all contribution plus interest of every year
print "When do you want to withdraw on it?"
time= int(raw_input())
# time in years
interest_rate = [.25,.1,.01]
#average return rate randomized

from random import choice

choice(interest_rate)

for x in range (0, time):
    wax_ball= contribution+(wax_ball*(1+interest_rate))
print round(wax_ball,2)
4

1 回答 1

0

如果您希望循环的每次迭代都有不同的速率,您可以使用:

for x in range (time):
    wax_ball= contribution+(wax_ball*(1+choice(interest_rate)))
于 2013-08-03T05:21:00.580 回答