我的代码如下:
a = 1
b = 2
c = 3
d = 4
e = 5
if ((a == 1 and
b == 2 and
c == 4) or
(d == 4 and e == 5)):
print "Yeah, Working"
else:
print "ooops"
可以轻松实现同样的代码吗?
答案 0 :(得分:9)
如果您希望条件更清晰或更好看,您可以这样做:
a = 1
b = 2
c = 3
d = 4
e = 5
if (a, b, c) == (1, 2, 4) or (d, e) == (4, 5):
print "Yeah, Working"
else:
print "ooops"
答案 1 :(得分:4)
这是一个非常个人化的答案,但我喜欢在if
语句之前初始化一些布尔值。
我觉得它更具可读性(因为你可以为你的变量赋一些有意义的名字),而且我非常确定编译器可以很容易地对它进行优化。
cond1 = a == 1 and b == 2 and c == 4
cond2 = d == 4 and e == 5
if cond1 or cond2:
print("Yeah, working")
else:
print("ooops")