首页 > 解决方案 > 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)

标签: pythonpython-3.x

解决方案


There are 2 different type of division in Python:

  1. Division
  2. 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

推荐阅读