Nov-22-2018, 03:46 PM
Hey everyone,
I'm trying to define a procedure that identifies if the length of each element in a list is the same length as the list. For example the list [[1,2],[3,4]] has a length of two and each element inside the list also has a length of two.
The problem I'm running into is how to return True or False using a 'for loop' for certain test cases.
For example in the below case, the list meets the criteria and returns true.
I'm trying to define a procedure that identifies if the length of each element in a list is the same length as the list. For example the list [[1,2],[3,4]] has a length of two and each element inside the list also has a length of two.
The problem I'm running into is how to return True or False using a 'for loop' for certain test cases.
For example in the below case, the list meets the criteria and returns true.
def correct_length(n): for e in n: if len(e) != len(n): break return False return True print correct_length([[1,2,3], [2,3,1], [3,1,2]])But, when the criteria is not met as in the next example, the output is still true.
def correct_length(n): for e in n: if len(e) != len(n): break return False return True print correct_length([[1,2,3], [2,3,1], [3,1,2,4]])I think the problem is probably something simple, but have been beating my head on the desk for a while trying to figure it out. Any help would be greatly appreciated.