年值随着年的减少随着年岁的增加而大大减少

时间:2018-09-06 15:43:37

标签: python python-3.x

我正在尝试制定一种算法来计算任何给定年份的剩余价值。我可以计算值,但无法弄清楚如何在Python 3中进行有效循环。

costs: P1=3000, P2=3100, P3=3000, P4=2900

p = 0.25

Value(year 1) = P1
Value(year 2) = (1 - p)P1 + P2
Value(year 3) = (1 - 2p)P1 + (1 - p)P2 + P3
Value(year 4) = (1 - 3p)P1 + (1 - 2p)P2 + (1 - p)P3 + P4
Value(year 5) = (1 - 3p)P2 + (1 - 2p)P3 + (1 - p)P4
Value(year 6) = (1 - 3p)P3 + (1 - 2p)P4
Value(year 7) = (1 - 3p)P4
Value(year 8) = 0

我需要能够在costs中拥有任意数量的值

到目前为止,我有这个:

number_years = 6
costs = {}
for year in range(number_years):
    costs[year + 1] = 3000
print(costs)

def repay_value_after_time(costs, year, percent):
    no_affected_years = int(1/percent)
    affected_years = [x for x in range(year - no_affected_years, year) if x in list(costs.keys())]
    print(affected_years)

    sum_prev_yrs = 0
    for i, prev_yr in enumerate(affected_years):
        multiplier = percent * (i + 1)
        sum_prev_yrs += costs[prev_yr] * multiplier
    try:
        return costs.get(year) + sum_prev_yrs
    except:
        return sum_prev_yrs

print(repay_value_after_time(costs, 5, 0.75))

似乎真的很混乱。它也不会将值减小为0,直到应该将其设为0的一年后;我该如何解决?

0 个答案:

没有答案