Python中每年的固定折旧表

2024-10-03 02:40:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我根据公式做了一个固定折旧表:

Fixed rate = 1 - ((salvage / cost) ^ (1 / life))

到目前为止,我有一段代码:

^{pr2}$

结果是:

Year       Depreciation       Book Value at the year-end
   1             $36904.27             63095.73444801933

我不知道如何让剩下的几年和第一年一起打印出来。如何使用我现在的代码打印出所有年份的折旧和帐面价值?在


Tags: the代码ratevalueyearat公式fixed
2条回答

我不确定我是否有正确的折旧公式(我被教过与你给出的不同的公式),但如果有必要,你可以调整这个逻辑。在

基本上,我所做的是将FixedRateDepreciationTable转换为generator function,这样它每年都会产生折旧和新价值,直到产品的使用寿命结束。在

在代码的主体部分,我迭代这个生成器并打印每个值。在

def FixedRateDepreciationTable(salvage, cost, life):
    rate = 1 - ((salvage / cost) ** (1 / life))

    for year in range(1, life + 1):
        dv = cost * rate
        cost -= dv
        yield year, round(dv, 2), cost


if __name__ == '__main__':
    print("Year\tDepreciation\tBook Value at the year-end")
    for year, depreciation, new_value in FixedRateDepreciationTable(1000, 100000, 10):
        print("{0:4}\t{1:>18}\t{2:26}".format(year, depreciation, new_value))

输出

^{pr2}$

使用循环:

for year in range(1, life):

缩进代码行以将它们放入循环中。在

相关问题 更多 >