How to have multiple conditions for a single if statement in python

So, I am writing some code in python 3.1.5 that requires more than one condition for something. 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 other than 0. Help?

+6
source share
5 answers

I would use

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

The operator and is identical to the logical gate with the same name; it will return 1 if and only if all inputs are equal to 1. You can also use or if you want this logical gate.

EDIT: , , . . , Python, .

+10

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

, , .

+2

, , , :

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

(, , , .)

0

Darian Moody :

a = 1
b = 2
c = True

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

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

all() True . , False.

python .

( - if python)

0

, , .

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

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

Everything that is multiplied by 0 == 0. If any of these conditions fails, then it evaluates to false.

-1
source

All Articles