0

a=int("5") b=list(range(1,10,2)) if a==b: print ("yes") else: print ("no")

I have wish to find out whether the given number is odd or even, what is wrong in my code?

30th Aug 2018, 2:17 PM
Girish Joshi
Girish Joshi - avatar
2 Antworten
+ 4
You are comparing an integer (a) with a list (b), this means the code will always output "no" because they are two different things. If you want to check whether a given number is even/odd you can use modulo or bitwise AND operator. # Check number be odd/even # using modulo operator num = 5 if num % 2 == 0: print("{} is even".format(num)) else: print("{} is odd".format(num)) # Check number be odd/even # using bitwise AND operator num = 7 if num & 1: print("{} is odd".format(num)) else: print("{} is even".format(num))
30th Aug 2018, 3:19 PM
Ipang
0
a=5 b=list(range(1,10,2)) for i in b: if a==i: print ("yes") break else: print ("no")
30th Aug 2018, 2:36 PM
Steppenwolf
Steppenwolf - avatar