1

I am fairly new to python programming, and recently I ran into this problem.

while(True):
panelType = input("Enter the type of panel[a, b, c, d]: ")
if(panelType.lower() != "a"
    | panelType.lower() != "b"
    | panelType.lower() != "c"
    | panelType.lower() != "d"):
    logger.error("Not a valid input. Try Again")
else:
    break

When I use bitwise operator I get this error: unsupported operand type(s) for |: 'str' and 'str'. However, once I changed it to OR operator, it worked well.

Could anyone explain why this occurred?

Thanks

1
  • 1
    Use if panelType.lower() not in set(['a', 'b', 'c', 'd']). Commented Nov 10, 2016 at 16:10

1 Answer 1

5

!= has lower precedence than | so it tried calculating "a" | panelType.lower() which makes no sense.

| is an operator meant for numbers, similar to * or +, so it makes sense you'd calculate it before making comparisons such as > or !=. You want or in this case, which has even lower precedence.

Better yet:

if panelType.lower() in ('a', 'b', 'c', 'd'):
Sign up to request clarification or add additional context in comments.

1 Comment

Cool! Thanks for the help. I tried to find answers in other posts, but I just didn't know what to search

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.