Zard
Zard

Reputation: 115

Why is "and" excluding any pairs (x,y) that have a 0 in them, rather than just excluding the one where both are 0?

I have a python 3 code that looks something like this:

for x in range(-1,2): 
    for y in range(-1,2):
        if x != 0 and y != 0:
            print("True")

Output looks like this:

True
True
True
True

It's supposed to output 8 "True"s. One for each permutation that isn't (0,0), when x and y both equal 0. Why is it outputting 4 instead?

Further investigation with replacing "print("True")" with "print(x,y)" outputs:

-1,-1
1,-1
-1,1
1,1

Where did the 0s go?

Upvotes: 0

Views: 54

Answers (1)

sujeet14108
sujeet14108

Reputation: 568

You should use "or"

for x in range(-1,2):
    for y in range(-1,2):
        if x != 0 or y != 0:
            print(x, y)
            print("True")

Upvotes: 3

Related Questions