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

djskj189
  • 285
  • 1
  • 5
  • 15

1 Answers1

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'):
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • Cool! Thanks for the help. I tried to find answers in other posts, but I just didn't know what to search – djskj189 Nov 10 '16 at 16:16