+ 3
Pls explain me the logic .why the output is male
sex="F" if(sex =="M" or "m"): print("Male") else: print("Female")
9 Answers
+ 9
AMAN TOMAR Don't use if sex=='M' or 'm' because that will always evaluate to True. 'm' as a non-empty string is true, so the whole expression is true.
if sex=='M' or sex=='m' checks if sex is either 'M' or 'm'
+ 6
if sex=='M' or sex=='m'
Or:
if sex.lower() == 'm'
+ 3
(sex=="M" or "m") return "m", not false
+ 2
Thank you Anna
+ 1
Anna still couldn't understand.can u pls explain in detail
+ 1
Yes i know the first condition but i want to understand why is the issue with the code that i have posted
0
if (sex=="M" or "m"):
# is equivalent to:
if ((sex=="M") or "m"):
# equivalent to:
if ((False) or True):
# equivalent to:
if True: