python - Why does the value produced is float and not integer?
问题描述
I have declared at line 1-3 for the value to be in integer. But when running the code, it'll give me float value instead of integer unless I declare it:
total = int(group_3 + group_2 + group_1)
Is this because of Modulo? If so, any idea why?
a = int(input())
b = int(input())
c = int(input())
group_1 = (((a % 2) + a) / 2)
group_2 = (((b % 2) + b) / 2)
group_3 = (((c % 2) + c) / 2)
total = (group_3 + group_2 + group_1)
print(type(total))
print(total)
解决方案
There are 2 different type of division in Python:
- Division
- Floor division
I will explain with example :
# 1. Division
a = 5
print(a/5)
Result will be :
2.5
But if you use Floor division:
# 2. Floor Division
a = 5
print(a//5)
Result will be :
2
推荐阅读
- cassandra - 解释 Cassandra 复制 json 字符串
- haskell - `coerce` 的所有类型检查都可以安全地替换为 `unsafeCoerce` 吗?
- r - 并排比较模型拟合的功能或包装?
- webpack - 意外令牌 (16:22) 您可能需要适当的加载程序来处理此文件类型
- bootstrap-4 - Bootstrap 4:如何使导航栏中的下拉链接头部可点击
- c++ - 什么时候调用全局对象的构造函数?
- python - 如何计算多边形内多个波段的平均像素值
- google-sheets - 我无法在谷歌表格中运行这个简单的代码
- c# - MainWindow 未订阅 UserControl 的事件
- sql-update - 根据选择更新房间