Python Forum
Returning true or false in a for loop
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.
Reply
#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
Reply
#3
Remove the break, and indent the return False to the same level as the break.
Reply
#4
Thanks a lot guys, I appreciate you clearing up my syntax confusion.

Peace,
Brian
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  returning values in for loop Nickd12 4 665 Dec-17-2020, 03:51 AM
Last Post: snippsat
  While True loop help Nickd12 2 545 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  path.exists returning True when it shouldn't natha18 0 368 Sep-21-2020, 01:04 PM
Last Post: natha18
  Is using while True loop good? escape_freedom13 5 776 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13
  While loop = False NectDz 1 697 Jun-03-2020, 04:35 PM
Last Post: GOTO10
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 910 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  While loop doesn't end when False Kanashi 2 920 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Do break operators turn while loop conditions from True to False? Drone4four 5 1,027 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  How to use while true/while loop on python christing 4 1,080 Oct-08-2019, 08:02 AM
Last Post: perfringo
  Returning True or False vs. True or None trevorkavanaugh 6 2,868 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020