I have tab that is a tuple with three tuples and each has 3 elements that can only be 1, -1 or 0. I want a function to receive a tab an evaluate if it is True or False. This is what I came up with:
tab = ((0,0,-1),(-1,1,0),(1,0))
def board(tab):
if len(tab) != 3:
return False
else:
for row in tab:
if len(row) != 3:
return False
else:
for element in row:
if element not in (-1, 0, 1):
return False
else:
return True
The problem is that with the current tab it says true and it should be false. Also if anything is something besides 1,-1 and 0 it should return False and if I say tab = ((0,0,-1),(-1,1,0),(1,0,2)) it returns True. What's the problem?
returnstatement will break the loop on the first iteration, which evaluates toTruebased on your current logic.True. You never actually test the contents of the third tuple.