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.
6 Antworten
+ 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".
+ 11
Try this
if(gender == 'M' or gender == 'm'):
print ("Male")
else:
print ("Female")
+ 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.
0
2 day
0
gender = 'M'
if (gender == "M" or gender == 'm'):
print('Male')
else:
print('Female')
This needs to be two statements to work.