10.4. While Continue

10.4.1. Syntax

  • if continue is encountered, it will jump to next loop iteration

>>> 
... while True:
...     continue

10.4.2. Example

>>> 
... while True:
...     answer = input('Continue? [y/n]')
...     if answer == 'n':
...         continue
...     ...
...     ...
...     ...

10.4.3. Use Case - 1

>>> TEXT = [
...     '# "Moon Speech" by John F. Kennedy',
...     '# Rice Stadium, Houston, TX, 1962-09-12',
...     '# Source: http://er.jsc.nasa.gov/seh/ricetalk.htm',
...     'We choose to go to the Moon.',
...     'We choose to go to the Moon in this decade and do the other things.',
...     'Not because they are easy, but because they are hard.',
...     'Because that goal will serve to organize and measure the best of our energies a skills.',
...     'Because that challenge is one that we are willing to accept.',
...     'One we are unwilling to postpone.',
...     'And one we intend to win',
... ]
>>>
>>> i = 0
>>>
>>> while i < len(TEXT):
...     line = TEXT[i]
...     i += 1
...     if line.startswith('#'):
...         continue
...     print(line)
...
We choose to go to the Moon.
We choose to go to the Moon in this decade and do the other things.
Not because they are easy, but because they are hard.
Because that goal will serve to organize and measure the best of our energies a skills.
Because that challenge is one that we are willing to accept.
One we are unwilling to postpone.
And one we intend to win

10.4.4. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: While Continue Lines
# - Difficulty: medium
# - Lines: 13
# - Minutes: 8

# %% English
# 1. Define `result: list[str]`
# 2. Use `while` to iterate over `DATA`
# 3. If line is empty, skip it
# 4. If line contains only spaces, skip it
# 5. If line starts with comment character `#`, skip it
# 6. Otherwise, add it to `result`
# 7. Use `continue` keyword
# 8. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: list[str]`
# 2. Użyj `while` do iteracji po `DATA`
# 3. Jeżeli linia jest pusta, to ją pomiń
# 4. Jeżeli linia ma tylko spacje, to ją pomiń
# 5. Jeżeli zaczyna się od znaku komentarza `#`, to ją pomiń
# 6. W przeciwnym wypadku dodaj linię do `result`
# 7. Use `continue` keyword
# 8. Uruchom doctesty - wszystkie muszą się powieść

# %% Hints
# - `Stop` or `Ctrl+C` kills infinite loop

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is list, \
'Variable `result` has invalid type, should be list'
>>> assert all(type(x) is str for x in result), \
'All elements in `result` must be a str'

>>> from pprint import pprint
>>> pprint(result)
['We choose to go to the Moon.',
 'We choose to go to the Moon in this decade and do the other things.',
 'Not because they are easy, but because they are hard.',
 'Because that goal will serve to organize and measure the best of our '
 'energies a skills.',
 'Because that challenge is one that we are willing to accept.',
 'One we are unwilling to postpone.',
 'And one we intend to win']
"""

DATA = ['',
    '# "Moon Speech" by John F. Kennedy',
    '# Rice Stadium, Houston, TX, 1962-09-12',
    '# Source: http://er.jsc.nasa.gov/seh/ricetalk.htm',
    ' ',
    'We choose to go to the Moon.',
    'We choose to go to the Moon in this decade and do the other things.',
    'Not because they are easy, but because they are hard.',
    'Because that goal will serve to organize and measure the best of our energies a skills.',
    'Because that challenge is one that we are willing to accept.',
    'One we are unwilling to postpone.',
    'And one we intend to win',
]

# Define `result: list[str]`
# Use `while` to iterate over `DATA`
# If line is empty, skip it
# If line contains only spaces, skip it
# If line starts with comment character `#`, skip it
# Otherwise, add it to `result`
# Use `continue` keyword
# type: list[str]
result = ...