Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Hello when I try to make a basic input panel I encounter with that error.

TypeError: unsupported operand type(s) for |: ‘str’ and ‘str’

ip = str(input(">>> "))
if ip == "0" | ip == "00":
    pass
elif ip == "1" | ip == "01":
    send()
elif ip == "2" | ip == "02":
    st()
elif ip == "3" | ip == "03":
    fst()
elif ip == "4" | ip == "04":
    res

Just WHY???

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

| is the bitwise-or operator. From Operator precedence, | binds tighter than ==, so

ip == "0" | ip == "00"

could be expressed as

ip == ("0" | ip) == "00"

str doesn’t support bitwise-or, so the "0" | ip fails. You wanted logical or. Since or has lower precedence than ==, this works

ip == "0" or ip == "00"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading