python - 检查python中的多个条件

标签 python python-2.7

我有以下代码:

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"

能否以简单且最佳的方式实现相同的代码?

最佳答案

如果你想让 if 条件更清晰或更好看,你可以这样做:

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"

关于python - 检查python中的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48908116/

相关文章:

amazon-web-services - AWS Lambda - Body Size is Too Large 错误,但 Body Size is Under Limit

python - 如何在 Python 中使用 Beautifulsoup 查找 div 内的所有 anchor 标签

python - 如何在没有管理员权限的情况下在 Windows 上安装 python

python - 类型错误 : argument of type 'function' is not iterable when starting django runserver

python - Django Rest Framework 使用 filterset_fields 在 ViewSet 中过滤计算出的 SerializerMethodField()

python - With 子句用于 Python 中的多处理

python - 如何在树莓派上安装 python 3 和 setuptools?

python - python如何识别参数的类型?

python - 如何从包含 munch 的字典中编写 YAML,而不使用 "!munch.Munch"?

python - 在 cmd 模块 python 中调用菜单