In the code in the description, if we input the value -27 how does the program print that it is odd?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

In the code in the description, if we input the value -27 how does the program print that it is odd??

n=int(input('Number?')) if n%2<=0: print ('the munber is even') elif n%2>0: print ('the number is odd') else: print('its just a number')

13th Sep 2016, 8:52 PM
--_--
--_-- - avatar
1 Answer
+ 1
That's how modulo division works in python. It always returns a number with the same sign as its second operand (2 in your case). In this case, -27 // 2 == -14, thus -27 % 2 == 1, and modulo division result can NEVER be less than 0 because the second operand is greater than 0 (2>0). Anyway, the logic is a little bit strange: if n is int, then n % 2 can only be 0 or 1. You can use strict equals check and remove else: branch.
13th Sep 2016, 10:15 PM
trueneu
trueneu - avatar