1

I get different results if I write "if X or y in var" than if I write "if x in var or if y in var." Why is that? I cannot find any documentation on this anywhere.

If, using the following Python3 code, I enter "rrrr" I get "whoohoo" not "WTF." Only if I remove the code "elif "read" or "road" in choice: print("whoohoo")," do I get "WTF." Can anyone enlighten me on what I'm missing about the difference in the two? As I say, I've searched broadly on the net and can't find anything on this. I'm expecting them both to return the same thing but they do not.

var = "read", "road"
print(var)
print("what is your choice?")

choice = input("> ")

if "raed"  in choice or "raad" in choice:
    print('Not Good')

elif "read" or "road" in choice:
    print("whoohoo")

elif "read" in choice or "road" in choice:
    print("excellent")

else:
    print("WTF")
khelwood
  • 55,782
  • 14
  • 81
  • 108
riverside
  • 11
  • 2

1 Answers1

3

This will check if x has a truthy value assigned to it (i.e. not empty), or else, if var contains the value for y

if x or y in var

This will check if var contains the value for x, or else, if var contains the value for y

if x in var or if y in var
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65