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))
我不确定我是否有正确的折旧公式(我被教过与你给出的不同的公式),但如果有必要,你可以调整这个逻辑。在
基本上,我所做的是将
FixedRateDepreciationTable
转换为generator function,这样它每年都会产生折旧和新价值,直到产品的使用寿命结束。在在代码的主体部分,我迭代这个生成器并打印每个值。在
输出
^{pr2}$使用循环:
缩进代码行以将它们放入循环中。在
相关问题 更多 >
编程相关推荐