如何解决如何创建具有变化值的列表?
如果我想通过定义一个带参数的函数来进行财务计算:
def FV(x,r,t):
其中 x 是初始金额,r 是比率,t 是年数,我如何编写一个函数来列出相应年份的所有金额?
到目前为止我最好的尝试是:
def FV(x,t):
"""
inputs:
x = amount invested
r = rate (in % p.a.) accrued on the account each year
t = number of years
returns a list "acc" with amount on the account at 0,1,... t
"""
acc = []
value = x*(1+r/100)**t
for years in range(t+1):
if years <= t:
acc.append(value)
return acc
print(FV(100,5,7))
解决方法
我不完全确定您想做什么,但听起来您正在寻找摊销表,而不是未来价值。
如果是这样,请尝试以下操作:
periods = 3
int_rate = .1
account = [100]
balance = account[0]
def amortize(periods,int_rate,balance):
while periods>0:
current_int = balance * int_rate
balance = balance+current_int
account.append(balance)
periods -=1
amortize(periods,balance)
account
输出:
[100,110.0,121.0,133.1]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。