-2

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 2
    no it is because you divide by 2 like this "/2" ... try "// 2" instead for integer division – Christian Sloper Feb 02 '21 at 08:49
  • Hope this will help you https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division – NoobCoder Feb 02 '21 at 08:50
  • https://stackoverflow.com/questions/1282945/python-integer-division-yields-float – Zhubei Federer Feb 02 '21 at 08:54
  • 1
    You can not in Python *declare a variable to be integer.* You are clearly expecting integer division. In Python 3 you achieve that by using the integer division operator `//` instead of `/`. You may have been led to expect integer division by an outdated textbook or web page. That is how it used to work in Python 2. – BoarGules Feb 02 '21 at 08:57
  • @NoobCoder If you think this question has an answer somewhere else in this site - [flag it as duplicate](https://stackoverflow.com/help/privileges/flag-posts) instead of posting a link as a comment... – Tomerikoo Feb 02 '21 at 09:02

1 Answers1

0

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
Hosseinreza
  • 561
  • 7
  • 18