Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Using the continue statement

Example 6.5. Using the continue statement

#!/usr/bin/python
# Filename: continue.py

while True:
s = raw_input('Enter something : ') if s == 'quit':

break
if len(s) < 3:
continue
print 'Input is of sufficient length' # Do other kinds of processing here...

Output

$ python continue.py
Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length Enter something : quit

How It Works

In this program, we accept input from the user, but we process them only if they are at least 3 characters long. So, we use the built-inlen function to get the length and if the length is less than 3, we skip the rest of the statements in the block by using thecontinue statement. Otherwise, the rest of the statements in the loop are executed and we can do any kind of processing we want to do here.

Note that thecontinue statement works with thefor loop as well.