Skip to content
CCC Python Course

while loops, break/continue, sentinels and EOF

Stage
1
Module
M1.6
Lessons
1

In this module

  • Repeat a block for as long as a condition holds, with while.
  • Read values until a sentinel value appears, ending the loop when it does.
  • Exit a loop early with break, and skip straight to the next pass with continue.
  • Recognize the EOFError a loop can raise reading past the end of input, and read every remaining line safely with import sys.

Before this module

Lessons

  1. 1while loops, break/continue, sentinels and EOF

Practice

Try these on the judges. Each link opens the problem on WMOJ or DMOJ.

  1. 2024 J2
    Dusa And The Yobis (opens on WMOJ in a new tab) WMOJ

    Grow a creature by feeding it smaller creatures, one at a time, until it meets one it cannot eat.

  2. 2021 J3
    Secret Instructions (opens on WMOJ in a new tab) WMOJ

    Follow a sequence of instruction codes until a stop code appears.

  3. 2020 J2
    Epidemiology (opens on DMOJ in a new tab) DMOJ

    Simulate an outbreak day by day until the total infected passes a limit.

    Why DMOJ: Tries the same kind of problem on DMOJ, which holds 2014 to 2020.