Reputation: 115
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 0
s go?
Upvotes: 0
Views: 54
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