Python Forum
While loop with List and if stmt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop with List and if stmt
#1
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
Reply
#2
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")
Reply
#3
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
Reply
#4
Var1 is declared as a set, and the order of the items may not be as you declared them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending to list of list in For loop nico_mnbl 2 2,379 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,413 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  loop through list or double loop 3Pinter 4 3,469 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,787 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,971 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,394 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020