0

I got this piece of code from a friend of mine and I was very confused. Is there any logic behind this or just python weirdness?

He asked: What do you think will be the output if the user enters "yes" in question?

question = input("This is just a question. Enter anything\n")
print("First answer") if "yes" or "no" in question else print("you dumb")

I obviously answered, it will print "First answer" on "yes" & "no".

Then he asked:
and if the user enters "lol"? what then?

I said "you dumb".

Then he said "it gave me 'first answer'".

Now, what do you think?

  • 1
    `"yes"` is a truthy value, so the condition is always true. The `"no" in question` is a red herring, because what's written is *not* the same as `"yes" in question or "no" in question`. – chepner Feb 11 '22 at 14:30
  • the condition in the expression `"yes" or "no" in quetion"` will always evaluate to `True` as it checks like `("yes") # non-empty string has bool True or ("no" in question)`. Thus, regardless the truth value of the latter it becomes `True or ?` -> `True`, so it will always print `"First answer"`. – null Feb 11 '22 at 14:33

0 Answers0