37

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")

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

SooBaccaCole
  • 383
  • 1
  • 3
  • 6

5 Answers5

44

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.

Agnel Vishal
  • 564
  • 6
  • 13
SSung2710
  • 582
  • 6
  • 8
  • My IDE said there was no syntax or other errors with it but when it runs it doesn't output the text properly. Roberto and your code, (which are one and the same,) both worked fine. – SooBaccaCole Apr 21 '16 at 01:55
40

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

a = 1
b = 2
c = True

rules = [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)

burkesquires
  • 1,345
  • 1
  • 14
  • 20
  • 4
    This is extremely useful when distinguishing multiple and statements that are independent from each-other by an or! Thank you for this! – nikooters Jun 14 '19 at 17:38
  • Thanks @nikooters! I could not agree more; it fit my needs exactly...the need that brought me to this questions. Cheers! – burkesquires Jun 15 '19 at 22:32
  • 2
    Note that there is no short-circuiting with this approach - all conditions are always evaluated. In a regular 'if' condition with 'and's, python would stop evaluating once it encountered the first false condition. – Kevin Hoffman Oct 01 '21 at 12:15
  • 1
    This should be the accepted answer. I spent several hours finding this simplified solution. Thanks a lot! – Akif Oct 29 '21 at 09:50
  • Hi @Akif, Thank you. Are there particular terms that we can add to this to help others find this answer easier? – burkesquires Oct 29 '21 at 17:34
  • 3
    I have searched as `python multiple if statements "and" condition best practices` and found many junky fundamental definitions. Finally I have found how `all` and `any` helps to define multiple if statements for `and` and `or` condition. Thanks! – Akif Oct 29 '21 at 21:57
  • Very enjoyable solution, thank you. – tail Oct 24 '22 at 12:39
2

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

Christian Henry
  • 172
  • 1
  • 5
2

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.

Elyria
  • 31
  • 2
0

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:

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.

no nein
  • 631
  • 3
  • 10
  • 27
  • 6
    horribly verbose and unreadable. See the answer above that uses the "all" method for a clean way to implement this – Evan Langlois Nov 04 '18 at 20:08
  • Yeah except the all statement does not allow for a specific number of conditions. Making it horribly simplistic and downright useless. Not to mention that you are not able to change the lists without changing the code, again simplistic and non-flexible. But thanks for your input – no nein Nov 05 '18 at 07:38
  • 1
    Please give an example, because I see no difference between what you have and what the example above it does. Both can change the conditions at runtime and I would think that needing a threshold number of conditions rather than all to be a rare occurrence, and not what the OP asked for! To duplicate your "at least one", use any() instead of all() – Evan Langlois Nov 25 '18 at 02:41
  • Neither of the answers can change the number of conditions, they can change the conditions themselves but not the number of them. – no nein Nov 25 '18 at 10:08
  • 1
    You can't add to the condition list? – Evan Langlois Nov 26 '18 at 11:05
  • Yes you can, but you are not able to e.g. change the number of conditions to return true if 3 out of 5 conditions are met. – no nein Dec 30 '19 at 15:02