+ 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!
7 Respostas
+ 4
~ swim ~
That makes sense, thanks!
+ 3
Seb TheS I still don't understand how the if clause can evaluate to True since sex = "F"
+ 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.
+ 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
+ 1
https://www.sololearn.com/discuss/1615887/?ref=app