Home > Software design >  Multiple condition if fonction inside a for loop
Multiple condition if fonction inside a for loop

Time:02-18

i'm browsing SO and i can't find a solution to my problem, so i made a post.

I try to make a For loop with a If statement inside that have multiple conditions (at least 3) but it does not work and i can't wrap my head around it... i tried lot of things but nothing seams to work.

here is my code, simplified because in my actual code the values for a, b, c and d are random :

    a=[1]
    b=[2]
    c=[3]
    d=[0,1,2,3,4,5,6,7,8]
    
    for i in d:
        if d>a and\
           d>b and\
           d>c:
            print("OK")
        else:
            print("not OK")

But no matter what i try, i always get this output :

not OK
not OK
not OK
not OK
not OK
not OK
not OK
not OK
not OK

Thank you for your help

CodePudding user response:

Do you mean this :

a = 1
b = 2
c = 3
d = [0, 1, 2, 3, 4, 5, 6, 7, 8]

for i in d:
    if i > a and i > b and i > c:
        print("OK")
    else:
        print("not OK")

not OK
not OK
not OK
not OK
OK
OK
OK
OK
OK

CodePudding user response:

Fixed code:

    a=1
    b=2
    c=3
    d=[0,1,2,3,4,5,6,7,8]
    
    for i in d:
        if i>a and\
           i>b and\
           i>c:
            print("OK")
        else:
            print("not OK")

Ouput

not OK
not OK
not OK
not OK
OK
OK
OK
OK
OK
  • Related