Python Program Taking Two Integer Inputs To Find Even And Odd
Here is the code. When I enter an even number (1st number) say 4 and odd number (2nd number) say 5 it prints '4 and 5 are even' num_1=int(input('first number ')) num_2=int(input('
Solution 1:
You were pretty close! The & operator in python is not the same as 'and' in python. 'and' tests that both conditions are logically true, while '&' is a bitwise operator that can satisfy conditions of logical trues, falses, and integers because these can be combined in bitwise, when 'and' just delineates logic.
num_1=int(input('first number '))
num_2=int(input('second number '))
if num_1%2==0 and num_2%2==0:
print(num_1,'and',num_2,'are even')
elif num_1%2!=0 and num_2%2!=0:
print(num_1,'and',num_2,'are odd')
elif num_1%2!=0 and num_2%2==0:
print(num_1,'is odd and ',num_2,'is even')
elif num_1%2==0 and num_2%2!=0:
print(num_1,'is even',num_2,'is odd')
else:
print('invalid entry')
Solution 2:
You've used the wrong operator: you used bit-wise "and" in place of logical "and". Bit-wise "and" has higher precedence, so your if statement is organized something like this:
if (num_1 % 2) == ((0 & num_2) % 2) == 0:
Change the &
items to the logical operator and
.
Solution 3:
&
is a bitwise operator while what you need is a logical operator and
the equivalent of &&
in Python
Post a Comment for "Python Program Taking Two Integer Inputs To Find Even And Odd"