首页 > 解决方案 > 我怎样才能正确循环这个乘法?

问题描述

我试图计算一个物体在 20 年内每年按其原始价值的 15% 折旧。问题是它只将第一次乘法的结果减去循环的其余部分,而不是将值减少 15% 20 次。

ovalue = int(input("Enter the value of the car: "))
res = ovalue * 0.15
fvalue = ovalue - res
year = 1
while (year <= 20):
   print("Year ",year,":", fvalue)
   year = year+1
   fvalue = fvalue-res

有没有什么办法解决这一问题?太感谢了

标签: python

解决方案


这将在每次迭代中将值减少当前值的 15%

value = int(input("Enter the value of the car: "))

year = 1
while year <= 20:
   value -= value * 0.15
   print("Year ",year,":", value)
   year = year+1

这将在每次迭代中将值减少 15% 的原始值(这可能不是您想要的)

value = int(input("Enter the value of the car: "))

deduction = value * 0.15
year = 1
while year <= 20:
   value -= deduction
   print("Year ",year,":", value)
   year = year+1


推荐阅读