示例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