Continue in "File as a sequence"

Hello codecampus!
I don’t seem to understand the following code (lines 4:5):

openfile = open('mbox.txt')
for line in openfile:
    line = line.rstrip()
    if not line.startswith("From"):
        continue
    print (line)

I would understand it if I was to use it without “continue”, to get all lines except the “From” lines. But the example doesn’t work like that.

Thanks in advance!

Source:

when continue executes, the print statement doesn’t, it avoids the lines that don’t start with “From”


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Thanks for the reply! I will be more careful with adding code on future posting.

In reference to your answer, doesn’t the code look exclusively for lines that start with “From”?

PS. Is it ok for me to share my “Arithmetic Formatter” code to review issues in another post/section?

this means that if the line doesn’t start with “From”, the code will jump to next iteration of the loop

startswith returns true/false, with the not in front that is inverted. In this case when startswith return false, the continue is executed.


Sure, open a topic for your Arithmetic Formstter if you need help

1 Like

I think I get it now, it will print all the lines; skipping those how do not have “From”.
Thankyou for your time and effort!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.