Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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('second number ')) 

if num_1%2==0 & num_2%2==0:
    print(num_1,'and',num_2,'are even')

elif num_1%2!=0 & num_2%2!=0:
    print(num_1,'and',num_2,'are odd')

elif num_1%2!=0 & num_2%2==0:
    print(num_1,'is odd and ',num_2,'is even')

elif num_1%2==0 & num_2%2!=0:
    print(num_1,'is even',num_2,'is odd')

else:
    print('invalid entry')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...