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
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 991 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  For Loop Returning 3 Results When There Should Be 1 knight2000 12 3,985 Sep-27-2021, 03:18 AM
Last Post: SamHobbs
  returning values in for loop Nickd12 4 11,861 Dec-17-2020, 03:51 AM
Last Post: snippsat
  While True loop help Nickd12 2 1,958 Oct-17-2020, 08:12 AM
Last Post: Nickd12
  path.exists returning True when it shouldn't natha18 0 1,458 Sep-21-2020, 01:04 PM
Last Post: natha18
  Is using while True loop good? escape_freedom13 5 3,465 Jul-03-2020, 08:27 PM
Last Post: escape_freedom13
  While loop = False NectDz 1 1,724 Jun-03-2020, 04:35 PM
Last Post: GOTO10
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,187 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  While loop doesn't end when False Kanashi 2 2,531 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,896 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

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