Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Returning true or false in a for loop
#1
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.

  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.
Quote
#2
The problem is that when the for loop ends, you return False, even though you should return True. The solution is to put the return False where the break is. The return will 'break' out of the function, serving the same purpose.

Another useful trick for similar but more complicated situations is using else on a for loop:

def check_len(sequence):
    for sub_list in sequence:
        if len(sub_list) != len(sequence):
            break
    else:
        return True
    return False
The else statement on the for loop triggers if the break statement was never used.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Remove the break, and indent the return False to the same level as the break.
Quote
#4
Thanks a lot guys, I appreciate you clearing up my syntax confusion.

Peace,
Brian
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Do break operators turn while loop conditions from True to False? Drone4four 3 88 Today, 05:28 AM
Last Post: buran
  How to use while true/while loop on python christing 4 184 Oct-08-2019, 08:02 AM
Last Post: perfringo
  Returning True or False vs. True or None trevorkavanaugh 6 476 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE
  file.write stops while true loop from executing in python3 boonr 3 339 Mar-25-2019, 12:50 PM
Last Post: ichabod801
  How to make loop go back to the beginning if test evaluaes to True. FWendeburg 1 370 Feb-13-2019, 01:26 AM
Last Post: stullis
  True == not False Skaperen 6 801 Aug-23-2018, 10:26 AM
Last Post: DeaD_EyE
  saving a true/false of a single value Skaperen 3 522 Aug-20-2018, 02:31 AM
Last Post: ichabod801
  Get True of false outside a loop morgandebray 2 468 Aug-09-2018, 12:39 PM
Last Post: morgandebray
  RegExp: returning 2nd loop in new document syoung 5 871 May-02-2018, 12:36 PM
Last Post: syoung
  True or false if running something? Artdigy 4 948 Mar-27-2018, 05:50 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)