Python-TypeError:|:'str'和'str'不支持的操作数类型

时间:2018-11-07 07:36:42

标签: python python-3.x operands

我正在尝试编写以下elif条件:

if(type = a):
    do this
elif(type=b|c):
    do this
else:
    do this

我在以下声明中遇到错误

elif(type=b|c)

错误:

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

任何人都可以建议我该怎么做。谢谢

3 个答案:

答案 0 :(得分:2)

这不是语法。

elif(type == b or type == c):

或:

elif(type in [b, c]):

答案 1 :(得分:2)

您的意思是(阅读评论):

if(type == a):
    do this
elif type in {b,c}: # i use `set` because it's the fastest # Also can do `type==b or type==c`
    do this
else:
    do this

答案 2 :(得分:1)

使用此:

if(type == a):
        do this
    elif (type == b OR type == c):
        do this
    else:
        do this