Get Got
Get Got

Reputation: 13

My for loop won't repeat the correct conditions

Is there anything wrong with my code? It should run about 500 times. But after it appends once it won't append again until the next elif condition is met.

for x in range(lengthOfListedContent):

    if listed_content[x][0] is books[0]:
        notes[0].append(listed_content[x][2])
        print(x)
    elif listed_content[x][0] is books[1]:
        notes[1].append(listed_content[x][2])
        print(x)
    elif listed_content[x][0] is books[2]:
        notes[2].append(listed_content[x][2])
        print(x)
    elif listed_content[x][0] is books[3]:
        notes[3].append(listed_content[x][2])
        print(x)
    else:
       print('fail')

This is the output I get:

0
1
fail
fail
5
fail
fail
fail
9
fail
fail

It should append at every iteration but it only appends at the first iteration of the met condition. I have no idea why it just skips until the next elif condition is met. I've been staring at the screen for hours.

Sorry if all this is convoluted, I'm still new..

Upvotes: 1

Views: 59

Answers (1)

omri_saadon
omri_saadon

Reputation: 10669

Using is between two arguments is comparing their ids instead of their values.

is tests for identity, not equality

Change all lines with is to ==

From:

if listed_content[x][0] is books[0]:

To:

if listed_content[x][0] == books[0]:

Upvotes: 2

Related Questions