0

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?

4
  • 1
    not all three tuples contain three elements though ? Commented Nov 22, 2020 at 21:15
  • 3
    Your return statement will break the loop on the first iteration, which evaluates to True based on your current logic. Commented Nov 22, 2020 at 21:17
  • 2
    How so? I there is a value other than 1,-1 and 0 then it should return False. If, like the tab I have above, there is only 2 elements in one of the tuples then it should also return False Commented Nov 22, 2020 at 21:19
  • 1
    Your first tuple fits all the criteria, so you return True. You never actually test the contents of the third tuple. Commented Nov 22, 2020 at 21:20

3 Answers 3

1

Your issue is your return statement of true, because of this return if any element is ok the entire function will return true, where in actuality you want the function to return false if any of the conditions are not met. You can remedy this by moving the return true to the end of the function so all of the elements must be tested before the function can be returned true. This is shown below and passes both of the test cases you mentioned

tab =  ((0,0,-1),(-1,1,0),(1,0,2))
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
        return True
Sign up to request clarification or add additional context in comments.

Comments

1

tab = ((0,0,-1),(-1,1,0),(1,0,2))

def board(tab): if len(tab) != 3: return False

for row in tab:
    if len(row) != 3: 
        return False

for element in row:
    if element not in (-1, 0, 1):
        return False
else:
    return True

Thanks guys!

Comments

1

You're looping through just one row!!! To understand, make the wrong row come first in the tuple and try it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.