如何在python中为一个if语句设置多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36757965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:17:43  来源:igfitidea点击:

How to have multiple conditions for one if statement in python

pythonfunctionif-statementargumentsconditional-statements

提问by SooBaccaCole

So I am writing some code in python 3.1.5 that requires there be more than one condition for something to happen. Example:

所以我在 python 3.1.5 中编写了一些代码,需要有多个条件才能发生。例子:

def example(arg1, arg2, arg3):
    if arg1 == 1:
        if arg2 == 2:
            if arg3 == 3:
                print("Example Text")

The problem is that when I do this it doesn't print anything if arg2 and arg3 are equal to anything but 0. Help?

问题是,当我这样做时,如果 arg2 和 arg3 等于 0 以外的任何值,它就不会打印任何内容。有帮助吗?

回答by SSung2710

I would use

我会用

def example(arg1, arg2, arg3):
     if arg1 == 1 and arg2 == 2 and arg3 == 3:
          print("Example Text")

The andoperator is identical to the logic gate with the same name; it will return 1 if and only if all of the inputs are 1. You can also use oroperator if you want that logic gate.

and操作者是相同的具有相同名称的逻辑门; 当且仅当所有输入为 1 时,它才会返回 1。or如果需要该逻辑门,也可以使用运算符。

EDIT: Actually, the code provided in your post works fine with me. I don't see any problems with that. I think that this might be a problem with your Python, not the actual language.

编辑:实际上,您帖子中提供的代码对我来说很好用。我不认为这有任何问题。我认为这可能是您的 Python 的问题,而不是实际语言的问题。

回答by burkesquires

Darian Moody has a nice solution to this challenge in his blog post:

Darian Moody 在他的博文中为这个挑战提供了一个很好的解决方案:

a = 1
b = 2
c = True

rules = [a == 1,
         b == 2,
         c == True]

if all(rules):
    print("Success!")

The all() method returns Truewhen all elements in the given iterable are true. If not, it returns False.

True当给定迭代中的所有元素都为真时,all() 方法返回。如果不是,则返回False

You can read a littlemore about it in the python docs hereand more information and examples here.

你可以阅读一点点在Python文档更多关于它在这里和更多的信息和例子在这里

(I also answered the similar question with this info here - How to have multiple conditions for one if statement in python)

(我也在这里用这个信息回答了类似的问题 - How to have multiple conditions for one if statement in python

回答by Elyria

Might be a bit odd or bad practice but this is one way of going about it.

可能有点奇怪或不好的做法,但这是一种解决方法。

(arg1, arg2, arg3) = (1, 2, 3)

if (arg1 == 1)*(arg2 == 2)*(arg3 == 3):
    print('Example.')

Anything multiplied by 0 == 0. If any of these conditions fail then it evaluates to false.

任何乘以 0 == 0 的值。如果这些条件中的任何一个失败,那么它的计算结果为 false。

回答by Christian Henry

Assuming you're passing in strings rather than integers, try casting the arguments to integers:

假设您传入的是字符串而不是整数,请尝试将参数转换为整数:

def example(arg1, arg2, arg3):
     if int(arg1) == 1 and int(arg2) == 2 and int(arg3) == 3:
          print("Example Text")

(Edited to emphasize I'm not asking for clarification; I was trying to be diplomatic in my answer. )

(编辑以强调我不是在要求澄清;我试图在我的回答中保持外交关系。)

回答by no nein

I am a little late to the party but I thought I'd share a way of doing it, if you have identical types of conditions, i.e. checking if all, any or at given amount of A_1=A_2 and B_1=B_2, this can be done in the following way:

我参加聚会有点晚了,但我想我会分享一种方法,如果您有相同类型的条件,即检查是否所有、任何或给定数量的 A_1=A_2 和 B_1=B_2,这可以通过以下方式完成:

cond_list_1=["1","2","3"]
cond_list_2=["3","2","1"]
nr_conds=1

if len([True for i, j in zip(cond_list_1, cond_list_2) if i == j])>=nr_conds:
    print("At least " + str(nr_conds) + " conditions are fullfilled")

if len([True for i, j in zip(cond_list_1, cond_list_2) if i == j])==len(cond_list_1):
    print("All conditions are fullfilled")

This means you can just change in the two initial lists, at least for me this makes it easier.

这意味着您可以更改两个初始列表,至少对我来说这更容易。