JensD
JensD

Reputation: 195

if statements, comparing lists, python

what am i doing wrong with my if statement that it doesn't recognise if an element in a is equal to 0? what i am attempting to print is for ever 0 the program prints . and for ever 1 #. cheers.

a=[0,0,1,0,1,1,0,1,1,0,0,0,0,1]
print(a)
for i in range(len(a)):
    if a[i]==[0]:
        print('.', end='')
    else:
        print('#', end='')
print()

bash:

[0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1]
##############

Upvotes: 0

Views: 3788

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601489

You probably want

if a[i] == 0:

instead of

if a[i] == [0]:

You want to compare the items to the integer value 0, not to the single-element list [0].

Upvotes: 3

Related Questions