• Advertisement
Sign in to follow this  

Python: How do I use the CONTINUE comand?

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm learning Python right now for my first language (besides a failed attempt to learn Blitz BASIC is 30 day) and I'm using the "A Byte if Python" tutorial when I came across the CONTINUE example. And like the coder I try to be, I thought up my own use for the piece of code, but when I ran it, it didn't run the same at all.
# My example
while True:
    x = raw_input('Guess a number, one through five: ')
    if x == 'next':
        break
    if int(x) == 5:
        continue
    print "Correct, type in 'next' for the next loop."

# His example
while True:
    s = raw_input('Enter something : ')
    if s == 'quit':
        break
    if len(s) < 3:
        continue
    print 'Input is of sufficient length (The end of the loop.)'

I found it did work when I changed "if int(x) == 5:" into an inequality, but I don't want an inequality for my piece of code. And lastly, I don't really understand the meaning of using the CONTINUE command. It seems to me, I could have just as easily made the loop "while x != 5:" or he could have made his loop "while len(s) < 3:" and both of us put our print statments at the bottom in an IF statment for before the loop rechecks its boolean. Could anyone give me a real-world example, only not one so advanced as would confuse a novice? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
continue example. It's in C++ but is applicable to Python as well.

It's a trivial example, but it's not too often that you really need to use a continue. However, it can also be very convenient in the right situations.

Share this post


Link to post
Share on other sites
It's not a "command"; it's a statement. I don't think I'll ever understand why beginners keep calling every language feature "a command", in every language. x.x

The difference is, if you make "his" loop 'while len(s) < 3:', then the first time that there is input that's long enough, it will *stop looping*. A 'continue' allows the loop to continue as long as the loop condition is met. It immediately goes back to the top of the loop, in the same way that 'break' immediately goes to the code just after the loop.

Also, a while loop checks the condition at the *top* of the loop. In the original example, 's' hasn't been assigned anything when the loop first starts, so you couldn't use 'while len(s) < 3' because it will try to call 'len' with something that doesn't yet have any meaning. (This will raise UnboundLocalError.)

Share this post


Link to post
Share on other sites
Ah I see, thank you both . . . but there is still one more question to be answered:

Why didn't my code work like his?
In his code, the loop reiterated at the continue statment as the boolean was false, but in mine in ignored the continue and went on no matter what the condition. Why?

Share this post


Link to post
Share on other sites
Continue has nothing to do with the loop condition. It merely says, "skip the rest of the code in this loop". Both examples here use the continue to skip the following print statement. 'sufficient length' is 3 or more, so he prints that out when the if check fails. You are doing the opposite, hoping for the print when the check is successful.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement