+ 3

Help me understand why this expression evaluates to True!

This is a challenge quiz question that I (obviously) got wrong. sex = "F" if (sex == 'M' or 'm'): print("Male") else: print("Female") The answer is 'Male', which means that (sex == 'M' or 'm') evaluates to True, right? Why? I can't get my head around this!

20th May 2019, 9:11 AM
S-Stefanova
S-Stefanova - avatar
7 Respostas
+ 4
~ swim ~ That makes sense, thanks!
20th May 2019, 9:30 AM
S-Stefanova
S-Stefanova - avatar
+ 3
Seb TheS I still don't understand how the if clause can evaluate to True since sex = "F"
20th May 2019, 9:19 AM
S-Stefanova
S-Stefanova - avatar
+ 2
No, you may replace (sex == 'M' or 'm') to (sex == 'M' or sex == 'm'), but there are 3 other ways: (sex in 'Mm') (sex.lower() == 'm') (sex.upper() == 'M') Anyways (sex == 'M' or 'm') doesn't always work, because it thinks 'm' as a condition, technically it would work like this: (sex == 'M' or bool('m')) And bool('m') is always True.
20th May 2019, 9:17 AM
Seb TheS
Seb TheS - avatar
+ 2
The if statement evaluate to true because there is a or. Yes the first comparition is false but second is true as Sed TheS wrote the letter m is considered to be true. See it in this code:https://code.sololearn.com/cDGZRObaNwCi/?ref=app
1st Jul 2019, 7:21 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
+ 1
https://www.sololearn.com/discuss/1615887/?ref=app
20th May 2019, 11:31 AM
Diego
Diego - avatar