0

For the code below:

string = "Hello everybody!"

if 'Hippo' or 'Monkey' in string:
    print("Found a hippo or monkey in my string")

the if statement is returning true. Why is this?

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
Vaevin
  • 1
  • 1
  • Tag the question with the language and compiler you're using. And make sure you format it so that it can be compiled by someone else. Please don't make others work harder to help you than is absolutely necessary. – Jeff Holt May 27 '22 at 15:48
  • 1
    What language would that be? But generally this condition should be written as `if 'Hippo' in string or ' Monkey' in string` in most languages – derpirscher May 27 '22 at 16:11
  • @derpirscher Ah sorry, that would be in Python. Thank you for helping! This looks to be working as intended now. – Vaevin May 27 '22 at 17:30
  • Please add the tag to the question. Don't just add a comment. The system understands tags. It doesn't use AI to read comments. – Raymond Chen May 27 '22 at 19:22
  • The semantics you seem to expect would require `... or ... in ...` to be a *single* expression, but it's not. It's just two separate expressions (combined with `or`, because `in` has higher precedence), and each expression has its own semantics the produce *values* that are then operated on by `or`. – chepner May 27 '22 at 19:32
  • Compare to `x < y < z`, which *is* a single expression, with three operands `x`, `y`, and `z`, rather than one `<` expression being used as the operand for a second. – chepner May 27 '22 at 19:33
  • Why did you close the question? It's a good question, you should allow answers not just comments – Elliptica May 27 '22 at 19:36
  • @Elliptica because the linked question handles more or less the same problem so it doesn't make any sense to add answers here when the correct answer is given already somewhere else ... – derpirscher May 27 '22 at 20:47

0 Answers0