Python Forum

Full Version: While loop with List and if stmt
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Unable to print "Yes". It's only printing "No". Can anyone please help me in the if part of the code, I think the error is there

Var1 = [27923320, 27547329, 21171382, 21463894, 18961555, 28432129]

i=0
while i < len(Var1):
    if i == [27923320, 27547329]:
        print("Yes")
    else:
       print("No")
    i += 1

Got it.

Var1 = [27923320, 27547329, 21171382, 21463894, 18961555, 28432129]

i=0
while i < len(Var1):
    if Var1[i] == 27923320 or Var1[i] == 27547329:
        print("Yes")
    else:
        print("No")
    i += 1
More readable way suggestion:

Var1 = [27923320, 27547329, 21171382, 21463894, 18961555, 28432129]

for item in Var1:
    if item in [27923320, 27547329]:
        print("Yes")
    else:
        print("No")
Thanks for the response.

I now created a set object "Var1" but why getting output not sorted?

Var1 = {27923320, 27547329, 21171382, 21463894, 18961555, 28432129}

for item in Var1:
    if item in [27923320, 27547329]:
        print("Yes", item)
    else:
        print("No")
Output:
Yes 27547329
No
No
No
No
Yes 27923320
Var1 is declared as a set, and the order of the items may not be as you declared them.