python - 如何制作一个简单的利息计算器
问题描述
我正在尝试挑战练习。我正在尝试使用输入来决定您将支付多少贷款。非常基本的东西 - 但是当我打印时,我会一遍又一遍地重复输入中回答的任何内容,我无法弄清楚我哪里出错了。我试图运行的代码是:
# $200 a month at 1.7% interest a year. Automate total based on months using user input.
months_given = input("How many months? ")
monthly_base = 200
yearly_tax = (1.7 / 100 / 12)
monthly_tax = (200 * yearly_tax)
monthly_total = int(monthly_tax + 200)
total = int(months_given * monthly_total)
print(f"You will need to pay: ${round(total, 2)}")
我尝试过使用 for/while 循环,但我还不精通这些循环,我仍在尝试了解它们的工作原理。
解决方案
你需要先解析你的input
然后使用它。当你使用input
它时返回str
。
在这一total = int(months_given * monthly_total)
行。months_given
isstr
并且当您使用*
运算符并且第二个操作数是int
,str
重复值。
正确的:
months_given = input("How many months? ")
months_given = int(months_given) # <-- here
monthly_base = 200
yearly_tax = (1.7 / 100 / 12)
monthly_tax = (200 * yearly_tax)
monthly_total = int(monthly_tax + 200)
total = int(months_given * monthly_total)
print(f"You will need to pay: ${round(total, 2)}")
推荐阅读
- javascript - html2canvas 图像从左侧切出,边距和空白在右侧
- reactjs - 无法在我的反应 js 应用程序中执行 PUT 请求
- typescript - 在 Typescript 中将参数声明为 Type 的目的
- linux - JMeter Perfmon 中的连接超时错误
- ios - UICollectionViewCell 可见时仍在配置
- java - getElementsByTagName 似乎在 jboss 6 上的 java 中不起作用
- html - 有没有办法在 CSS 中设计同学图标
- html - 为什么切换按钮在引导导航栏中不起作用?
- mongodb - ref: 如何知道 mongoose 中的 ref?
- angular - 如何为 Ag 网格 Angular 中的空单元格添加值