+ 3

Pls explain me the logic .why the output is male

sex="F" if(sex =="M" or "m"): print("Male") else: print("Female")

7th Apr 2019, 5:44 PM
AMAN TOMAR
AMAN TOMAR - avatar
9 Respostas
+ 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'
7th Apr 2019, 6:13 PM
Anna
Anna - avatar
+ 6
if sex=='M' or sex=='m' Or: if sex.lower() == 'm'
7th Apr 2019, 6:08 PM
Anna
Anna - avatar
+ 3
(sex=="M" or "m") return "m", not false
8th Apr 2019, 7:43 AM
Sergey Soloboev
Sergey Soloboev - avatar
+ 2
Thank you Anna
7th Apr 2019, 6:15 PM
AMAN TOMAR
AMAN TOMAR - avatar
7th Apr 2019, 6:06 PM
AMAN TOMAR
AMAN TOMAR - avatar
+ 1
Anna still couldn't understand.can u pls explain in detail
7th Apr 2019, 6:10 PM
AMAN TOMAR
AMAN TOMAR - avatar
+ 1
Yes i know the first condition but i want to understand why is the issue with the code that i have posted
7th Apr 2019, 6:12 PM
AMAN TOMAR
AMAN TOMAR - avatar
0
if (sex=="M" or "m"): # is equivalent to: if ((sex=="M") or "m"): # equivalent to: if ((False) or True): # equivalent to: if True:
3rd Oct 2019, 12:31 AM
Aymen