python - 如何使用 while 循环每月将值增加 5%?
问题描述
我正在尝试获取起始余额并每月将值增加 5%。然后我想在下个月将这个新的平衡反馈到方程式中。我尝试使用 while 循环来做到这一点,但它似乎并没有重新输入新的平衡。
我使用 60 个月(5 年)的公式,但这可以改变
counter = 1
balance = 1000
balance_interest = balance * .05
while counter <= 60:
new_monthly_balance = (balance + balance_interest)*(counter/counter)
print(new_monthly_balance)
balance = new_monthly_balance
counter += 1
解决方案
你永远不会改变balance_interest
循环。
你打算做*(counter/counter)
什么?这只是乘以 1.0,这是一个空操作。
while counter <= 60:
balance *= 1.05
print(balance)
counter += 1
更好的是,既然您知道要迭代多少次,请使用 a for
:
for month in range(60):
balance *= 1.05
print(balance)
顺便说一句,什么样的金融每月有 5% 的持续增长???
推荐阅读
- javascript - 每次添加时如何用对象数组填充html表
- android - Kivy 应用程序在开发中运行良好,但在生成发布时抛出错误
- docker - 在 Ubuntu 20.04 中启动时禁用 Docker
- python - 从字符串中提取日期并保存在新的 pandas DataFrame 列中
- c# - 如何在 XAML 和 C# (UWP) 中进行应用范围的明暗模式切换?
- python - 如何在 CSV 文件中搜索文本并打印到新的 CSV 文件
- java - 将 Spring Boot 后端 + oauth 与前端应用程序集成的问题
- excel - VBA 公式数组 A1 或 R1C1
- python - 与 `filter(not function, iter)` 相比,使用 `itertools.filterfalse()` 有什么效率优势吗?
- reactjs - 如何导入 scss 文件并在 React 应用程序中全局使用它们?