Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...