Python Forum
While loop with List and if stmt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: While loop with List and if stmt (/thread-13603.html)



While loop with List and if stmt - alinaveed786 - Oct-23-2018

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



RE: While loop with List and if stmt - ODIS - Oct-23-2018

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



RE: While loop with List and if stmt - alinaveed786 - Oct-23-2018

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



RE: While loop with List and if stmt - jdjeffers - Oct-23-2018

Var1 is declared as a set, and the order of the items may not be as you declared them.