0

I am trying to test if an input (a string) is equivalent to 3 different strings ( Only has to be equivalent to one of them). But no matter what input I put in it always comes out equivalent unless I remove the or statements. Can someone explain this to me???

Input_String = 'Random String'

Option_1 = 'rock'

Option_2 = 'paper'

Option_3 = 'scissors'

if Input_String == Option_1 or Option_2 or Option_3:

   print(1)

else:

    print(2)

# It will always print 1 like this.
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271

1 Answers1

0

The keywords or and connect boolean values (True, False). In your case

If a == b or c or d:

the interpreter would understand (a == b) as boolean value weather a is equal to b, c as boolean value True (as non empty string is converted to boolean type as True)n and same for d. So you will get

If (a == b) or True or True:

So if there is at least one True the whole statement will have value of True, and the body of If statement will be interpreted.

The right way is.

if Input_String == Option_1 or Input_String == Option_2 or Input_String == Option_3: 
     print(1)