0

Why Male is the output and not female🤔

CODE 👇 gender = 'F' if(gender == 'M' or 'm'): print('Male') else: print ('Female') OUTPUT👇 Male . . Why output Male and not Female 🤔 Anyone can pls explain it.

12th Apr 2021, 4:08 AM
Silent
6 odpowiedzi
+ 6
Silent because if we have truthy value in if statement, if block gets executed. Here gender== 'M' or 'm' (gender=="M" is False but "m" is truthy value and False or True yields True) corresponds to boolean value True, that's why it prints "Male".
12th Apr 2021, 4:14 AM
CHANDAN ROY
CHANDAN ROY - avatar
+ 11
Try this if(gender == 'M' or gender == 'm'): print ("Male") else: print ("Female")
12th Apr 2021, 4:12 AM
Vinayak
Vinayak - avatar
+ 2
Silent Learn again about python syntax.
12th Apr 2021, 4:17 AM
A͢J
A͢J - avatar
+ 2
I can deduce what the other answers refer to, but they don't help. You have 2 if statements (in Python) where in English it would be only 1 if statement. Let's break it down: if(gender == 'M' or 'm'): Let's look at that line specifically as it is quite complex. We have: if(): gender == 'M' or 'm' That line has 4 parts to it! Wow! And it works! Great job. We have an if, 2 statements, and an operator. Now look at that first statement: gender == 'M' Great work! You added in a comparison to see if gender is equivalent to 'M.' This is false in this case. Now breaking that from the next statement is the operator or. Let's then look at the second statement: 'm' It is too simple for your test case, and needs to have a comparison added to it, too. This is what others call a 'truthy' statement for fun. Maybe just add the same comparison as the first statement, or you could make it more robust and allow for more genders. This snip of a single line of your code, 3 characters, is why male is the output.
12th Apr 2021, 5:09 AM
Phil Andy Graves
Phil Andy Graves - avatar
0
2 day
13th Apr 2021, 1:12 PM
Hajarat Olamide
Hajarat Olamide - avatar
0
gender = 'M' if (gender == "M" or gender == 'm'): print('Male') else: print('Female') This needs to be two statements to work.
25th Aug 2023, 2:03 PM
Daniel Wray
Daniel Wray - avatar