0

I am trying to break a for loop that checks reads lines in another module. I notice that this function runs through every line of code in the module, but it only returns the boolean for the last line... how can I stop the loop as soon as it sees a false WITHOUT USING the break function? You don't have to worry about opening the file, I am capable of doing that

read_lines = file_handle.readlines()

for next_line in all_lines:

    check = next_line[0]

    if (check == ' '):
        result = False

    else:
        result = True
return result
5
  • WITHOUT USING the break function but WHY ? However you may enclose your for loop inside a function and return it, if you really don't wanna use break Commented Oct 29, 2017 at 7:35
  • return False if check == ' ' else True Commented Oct 29, 2017 at 7:40
  • A simple return False will do the job. Commented Oct 29, 2017 at 7:40
  • What do you mean by that? I heard in computer science, breaks should never be used... Commented Oct 29, 2017 at 7:41
  • @KlausD.Is there a way of doing it with only one return statement? Commented Oct 29, 2017 at 7:44

2 Answers 2

1

It's important to understand what exactly you want to return, and under what conditions, because this greatly affects what your function returns and when. For example, do you want to check if check == ' ' for even a single case? In which case, one return is enough.

for next_line in all_lines:
    if next_line[0] == ' ':
        return False

return True

Alternatively, you can use any:

return not any(x[0] == ' ' for x in all_lines)

Or, its counterpart, all, with an inverted condition:

return all(x[0] != ' ' for x in all_lines)

Note that str.isspace() is a more pythonic way of checking if a string is a whitespace character, although that makes a check for all whitespace characters (including tabs and newlines, not just whitespace characters).

Sign up to request clarification or add additional context in comments.

6 Comments

Uhm... since I'm running through all the lines, I'm guessing it's more than one case? I wanna use your 3rd method, but I don't think it works for multiple cases?
Could you explain why it doesn't output return true when it gets run passed? I don't understand the algorithm behind this
@DaquarisGeorgino It's an all or nothing approach. Either all the tests pass and you return True, or even one test fails and you return False.
I see... so if even one tests fails, it returns false? but wouldn't the return true afterwards overwrite the return false?
@DaquarisGeorgino not so. Because once a function returns, it can't come back and return something else like that.
|
0

You can add a return statement in your code , which will exit function as soon as it finds a match

read_lines = file_handle.readlines()

for next_line in all_lines:

    check = next_line[0]

    if (check == ' '):
        result = False
        #add a return here
        return result
    else:
        result = True

return result

5 Comments

Is there a way to do it with only one return statement?
@cᴏʟᴅsᴘᴇᴇᴅ : Can you plz explain what is not correct ? You mean to say it has two return statements ?
I mean to say you have probably not understood what OP is trying to do. See my answer for details. Cheers.
@cᴏʟᴅsᴘᴇᴇᴅ : His question is not to use break later he mentioned not to use two return statements. Anyway my answer is using two return statements
Yeah but OP doesn't know what they want, and sometimes it's more important to give OP what they need, not what they want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.