Wednesday, 4 May 2016

python - Why does `a == b or c or d` always evaluate to True?

I am writing a security system that denies access to unauthorized users.



import sys

print("Hello. Please enter your name:")
name = sys.stdin.readline().strip()
if name == "Kevin" or "Jon" or "Inbar":
print("Access granted.")

else:
print("Access denied.")


It grants access to authorized users as expected, but it also lets in unauthorized users!



Hello. Please enter your name:
Bob
Access granted.



Why does this occur? I've plainly stated to only grant access when name equals Kevin, Jon, or Inbar. I have also tried the opposite logic, if "Kevin" or "Jon" or "Inbar" == name, but the result is the same.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...