TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'

Or Halimi

I got strange error when I tried to use "|" operator in if with tuple.

#example
Myset = ((1, 3), (4, 6), (3, 1), (2, 2), (3, 5), (2, 4), (3, 3))
courd = (4,6)
if(courd[0] - 1 ,courd[1] - 1 in d ):
    isSafe = True # work as expected

but if I will try something like this:

if((courd[0] - 1 ,courd[1] - 1 in d) | 2==2 ): # or something else that involved "|" 
    isSafe = True 

I'm getting

Traceback (most recent call last):
  File "<pyshell#87>", line 1, in <module>
    if((courd[0] - 1 ,courd[1] - 1 in d )| (2 == 2)):
TypeError: unsupported operand type(s) for |: 'tuple' and 'bool'
thefourtheye

You need to use parens, as needed, like this

if((courd[0] - 1, courd[1] - 1) in d):
    pass

Now, it will create a tuple (courd[0] - 1, courd[1] - 1) and it will check if it is in d. In the next case,

if((courd[0] - 1, courd[1] - 1 in d) | 2 == 2):
    pass

(courd[0] - 1, courd[1] - 1 in d) will be evaluated first and that will create a tuple. And then 2 == 2 will be evaluated (since | has lower precedence than ==) to True, which is basically a boolean. So, you are effectively doing

tuple | boolean

That is why you are getting that error.

Note: | is called bitwise OR in Python. If you meant logical OR, you need to write it like this

if(((courd[0] - 1, courd[1] - 1) in d) or (2 == 2)):
    pass

Now, (courd[0] - 1, courd[1] - 1) will be evaluated first to create a tuple and then the tuple will be checked if it exists in d (this will return either True or False, a boolean) and then (2 == 2) will be evaluated which returns True. Now logical or would happily work with two booleans.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for |: 'bool' and 'Q'

From Dev

TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' with random generator?

From Dev

Unsupported operand types 'NoneType'

From Dev

Django unsupported operand types

From Dev

operand types are incompatible ("bool (*)()" and "bool")

From Dev

unsupported operand types for //: 'str', 'int'

From Dev

unsupported operand types for * : 'float' and 'decimal'

From Dev

TypeError: unsupported operand type in Python

From Dev

CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

From Dev

CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

From Dev

unsupported operand type(s) for +: 'int' and 'tuple'

From Dev

Python: unsupported operand type(s) for /: 'tuple' and 'float'

From Dev

Unsupported operand types for bitwise exclusive OR in Python

From Dev

TypeError: unsupported operand type(s) for +=: 'int' and 'list'

From Dev

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

TypeError: unsupported operand int and NoneType? Python

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for |: 'int' and 'str'

From Dev

TypeError: unsupported operand type(s) for |: 'list' and 'list'

From Dev

TypeError: unsupported operand type(s) for -: 'list' and 'list'

From Dev

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

From Dev

TypeError: unsupported operand type(s) for +: 'int' and 'Element'

From Dev

TypeError: unsupported operand type(s) for //: 'int' and 'graph'

From Dev

TypeError: unsupported operand type(s) for -: 'module' and 'LinearSegmentedColormap'

Related Related

  1. 1

    TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

  2. 2

    TypeError: unsupported operand type(s) for |: 'bool' and 'Q'

  3. 3

    TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

  4. 4

    TypeError: unsupported operand type(s) for %: 'Text' and 'tuple'

  5. 5

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' with random generator?

  6. 6

    Unsupported operand types 'NoneType'

  7. 7

    Django unsupported operand types

  8. 8

    operand types are incompatible ("bool (*)()" and "bool")

  9. 9

    unsupported operand types for //: 'str', 'int'

  10. 10

    unsupported operand types for * : 'float' and 'decimal'

  11. 11

    TypeError: unsupported operand type in Python

  12. 12

    CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

  13. 13

    CuPy and Dirichlet gives me TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

  14. 14

    unsupported operand type(s) for +: 'int' and 'tuple'

  15. 15

    Python: unsupported operand type(s) for /: 'tuple' and 'float'

  16. 16

    Unsupported operand types for bitwise exclusive OR in Python

  17. 17

    TypeError: unsupported operand type(s) for +=: 'int' and 'list'

  18. 18

    TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

  19. 19

    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  20. 20

    TypeError: unsupported operand int and NoneType? Python

  21. 21

    TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

  22. 22

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

  23. 23

    TypeError: unsupported operand type(s) for |: 'int' and 'str'

  24. 24

    TypeError: unsupported operand type(s) for |: 'list' and 'list'

  25. 25

    TypeError: unsupported operand type(s) for -: 'list' and 'list'

  26. 26

    TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

  27. 27

    TypeError: unsupported operand type(s) for +: 'int' and 'Element'

  28. 28

    TypeError: unsupported operand type(s) for //: 'int' and 'graph'

  29. 29

    TypeError: unsupported operand type(s) for -: 'module' and 'LinearSegmentedColormap'

HotTag

Archive