0

Why is the output “male” and not “female”?

Hello. The answer is probably very obvious but I don’t get it. Why does it output “Male” here? Isn’t the code supposed to proceed to the next “else”? https://code.sololearn.com/cP13S17hRQ1c/?ref=app

9th Jul 2022, 2:47 AM
Hamsdino
Hamsdino - avatar
14 odpowiedzi
+ 6
Hamsdino You must explicitly express your comparison to get a a correct boolean result. You did not compare the "m" against sex, so it elevated as True in your first condition, thus printing Male sex = "F" if (sex == "M" or sex =="m"): print("Male") else: print("Female")
9th Jul 2022, 2:55 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 7
Rik Wittkopp are you advising to fix the code with: if (sex == ("M" or "m")):? That would fail. Due to short circuit logic, that reduces to: if (sex == ("M")): and would not work as intended when sex = "m". Better advice would be: if sex in ("M", "m"):
9th Jul 2022, 5:36 AM
Brian
Brian - avatar
+ 5
Hamsdino You could also use paranthesis to group your conditions together for the single comparison sex = "F" if (sex == ("M" or "m")): print("Male") else: print("Female")
9th Jul 2022, 2:56 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
Python interprets everything except 0 as True. I think the reason is because it exists
9th Jul 2022, 3:20 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 4
@Hamsdino, any non empty string is evaluated to True, only empty string is considered False. See following page for more examples: https://www.freecodecamp.org/news/truthy-and-falsy-values-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
9th Jul 2022, 7:55 AM
Maczeta
Maczeta - avatar
+ 4
=> to get familiar with True, False, as well as with truthy and falsy values or expressions, i recommend to read: Truthy and Falsy Values in Python: A Detailed Introduction: https://www.freecodecamp.org/news/truthy-and-falsy-values-in-python/ in short: all of them are falsy values: data structures and types: empty lists, tuples, dictionaries, sets, strings, bytes, ranges numbers: zero of integers, floats, complex objects ...
9th Jul 2022, 10:38 AM
Lothar
Lothar - avatar
+ 3
Rik Wittkopp Thanks that makes sense! Do you know why a single letter is interpreted as True? Does it get interpreted as “m” == “m”?
9th Jul 2022, 3:16 AM
Hamsdino
Hamsdino - avatar
9th Jul 2022, 6:07 AM
Adebayo Abdul Salam
Adebayo Abdul Salam - avatar
+ 2
Phyton Abdul Salam Thanks! Maczeta and Lothar Thanks, very useful link!
14th Jul 2022, 12:34 AM
Hamsdino
Hamsdino - avatar
+ 1
sex = input() print("Male") if sex in ("M", "m") else print ("Female")
9th Jul 2022, 6:12 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
cause coding ain't about genders
10th Jul 2022, 9:25 AM
LIMO
LIMO - avatar
0
Brian Thanks for fixing that second option. This one works. So there is no way to use “or” in this case?
14th Jul 2022, 12:29 AM
Hamsdino
Hamsdino - avatar
0
LIMO lmao it was just a code challenge
14th Jul 2022, 12:36 AM
Hamsdino
Hamsdino - avatar
0
Uwltoamym Smart advice
14th Jul 2022, 12:37 AM
Hamsdino
Hamsdino - avatar