0

so I am quite a beginner and I am truly stumped.

I am trying to check if a certain string is inside a variable that is given as a user input, and yet everytime I do so, python always confirms that what I am checking for is inside the string even if it is not. Could anyone give some guidance.

ex:

path = raw_input(">").lower()
print path, "THIS IS PATH" #just doing this to make sure my path variable is updating

if "stair" or "way" or "rect" in path:
    print "Traveling to room5" #TEST
    room5()
elif "rope" or "pent" in path:
    print "Traveling to room1" #TEST
    room1()
else:
    print "Error, restarting room."
    room2()

and yet when I give the script for example "rope" it will still complete the if statement and print "Traveling to room5".

What am I doing wrong here?

1 Answers1

0

It should be -

if "stair" in path or "way" in path or "rect" in path:

In your case stair evaluates to be true, hence the condition is short circuited, and evaluates as true every time. stair is valuated is true because it is non-zero.

Like I said, conditions after "stair" are not even evaluated in your case -

>>> if 'stair':
...   print "hey"
...
hey
>>> if 'stair' or 0:
...   print "hey"
...
hey
theharshest
  • 7,767
  • 11
  • 41
  • 51