如何在python中为一个if语句提供多个条件 [英] How to have multiple conditions for one if statement in python

查看:6218
本文介绍了如何在python中为一个if语句提供多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在python 3.1.5中编写了一些代码,要求有一个以上的条件才能发生。示例:

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

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

问题在于,当我这样做时,如果arg2和arg3等于0,则不会打印任何内容。帮助?

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

推荐答案

我会用

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

和运算符与具有相同名称的逻辑门相同;当且仅当所有输入都是1时,它才会返回1.你也可以使用或者如果你想要那个逻辑门。

The and operator 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 or if you want that logic gate.

编辑:实际上,提供的代码是你的帖子对我很好。我没有看到任何问题。我认为这可能是你的Python的问题,而不是实际的语言。

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中为一个if语句提供多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆