小编dzn*_*zny的帖子

Python OR运算符和括号

示例1 - 这有效:

def thisorthat():
    var = 2

    if (var == 3 or var == 2):
        print "i see the second value"
    elif (var == 2 or var == 15):
        print "I don't see the second value"

thisorthat()
Run Code Online (Sandbox Code Playgroud)

示例2 - 这不起作用:

def thisorthat():
    var = 2

    if var == (3 or 2):
        print "i see the second value"
    elif var == (2 or 15):
        print "I don't see the second value"

thisorthat() # "I don't see the second value"
Run Code Online (Sandbox Code Playgroud)

有没有办法将变量与"OR"运算符进行比较,而不是在每一行中重复两次变量?

python

1
推荐指数
1
解决办法
157
查看次数

标签 统计

python ×1