0

Python challenge question("male" or "female")

Sex = "F" if (Sex == "M" or "m"): Print("male") else: Print("female") #CAN SOMEBODY EXPLAIN WHY THE ANSWER IS MALE INSTEAD OF FEMALE? #i didn't create this question btw😂I got this question and I got wrong in the challenge

16th May 2020, 3:46 AM
Burn
Burn - avatar
5 Answers
0
that happend through the incorrect use of binary operators. if Sex == 'M' or Sex == 'm'
16th May 2020, 3:59 AM
Emanuel Maliaño
Emanuel Maliaño - avatar
+ 5
Good question and for the explanation of such kind of answer refer this... https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true
16th May 2020, 4:00 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 1
Python evaluates any non-empty string, list, tuple, dict as True, also any non-zero number is evaluated as True when used with IF or a logical operator (OR,AND,NOT) This is why "m" is evaluated as True, and you have to do the == comparison
16th May 2020, 8:19 AM
NourEddine Yassine
0
#Python challenge question("male" or "female") Sex = "F" if (Sex == "M" or Sex == "m"): print("male") else: print("female") # use print in lowercase, # condition must be written properly!
16th May 2020, 3:53 AM
Manish
Manish - avatar
0
Kiran Deep Naidu Emanuel Maliaño Manish thanks everyone....I got this question in a challenge and got wrong so now I know and the Print is because it just auto corrects me when I write here and I didn't realize and I didn't downvote u guys btw 😂
16th May 2020, 4:32 AM
Burn
Burn - avatar