python - 我怎样才能正确循环这个乘法?
问题描述
我试图计算一个物体在 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
有没有什么办法解决这一问题?太感谢了
解决方案
这将在每次迭代中将值减少当前值的 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
推荐阅读
- firebase - 如何在 Firebase 中安装 Namecheap SSL?
- python - 使用子文件夹中的年份更新 .txt 文件
- javascript - 如何在此代码中嵌入使用文件选择器而不是指定图像源?
- google-sheets - 如何检查一系列时间戳是否在时间范围内(谷歌表格)
- python - 绘制垂直线错误的 X 值
- python - BeautifulSoup 保留一些文本,但删除标签的其余部分
- node.js - 如何使用 Lambda 从 DynamoDB 中的列表中删除对象
- gwt - 如何摆脱这个无用的 GWT 编译器警告?
- asp.net-mvc - 在 MVC 5 Razor 页面中使用 VB.Net 模块
- python - 如何从块列表中提取列表?