How to have multiple conditions for one if statement in python [duplicate] How to have multiple conditions for one if statement in python [duplicate] python python

How to have multiple conditions for one if statement in python [duplicate]


I would use

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

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

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.


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

a = 1b = 2c = Truerules = [a == 1,         b == 2,         c == True]if all(rules):    print("Success!")

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

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

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


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. 🙂)