Bottom Page

Thread Rating:
• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Returning true or false in a for loop bbop1232012 Unladen Swallow Posts: 2 Threads: 1 Joined: Nov 2018 Reputation: 0 Likes received: 0 #1 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. ``` 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. ichabod801 Bunny Rabbit Posts: 3,826 Threads: 82 Joined: Sep 2016 Reputation: 231 Likes received: 1160 #2 Nov-22-2018, 04:02 PM (This post was last modified: Nov-22-2018, 04:06 PM by ichabod801. Edited 1 time in total.) 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 woooee Minister of Silly Walks Posts: 449 Threads: 0 Joined: Feb 2018 Reputation: 33 Likes received: 36 #3 Nov-22-2018, 04:02 PM Remove the break, and indent the return False to the same level as the break. bbop1232012 Unladen Swallow Posts: 2 Threads: 1 Joined: Nov 2018 Reputation: 0 Likes received: 0 #4 Nov-22-2018, 04:44 PM Thanks a lot guys, I appreciate you clearing up my syntax confusion. Peace, Brian « Next Oldest | Next Newest »

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)