如何解决在Python中使用//运算符除以零
运行以下代码:
p = int(input("principal: "))
interest = int(input("annual interest rate: "))
years = int(input("# of years: "))
r = interest // 100
n = years * 12
top = r * (1 + r) ** n
bottom = (1+r) ** n - 1
fraction = top // bottom
A = fraction * p
print(A)
我得到:
line 17,in <module>
fraction = top // bottom
ZeroDivisionError: integer division or modulo by zero
我是初学者,请保持友好!
解决方法
下限除法//
将结果四舍五入到最接近的整数(数据类型= 整数),而r
作为整数,当{{1} }是 。
如果interest
等于top
,您会看到r
会发生什么。
与0
和fraction
运算符相同。使用//
对数字进行四舍五入。
round
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。