Paul's Python CoursePPC

Sentinels

Not met yet

A sentinel is a special value that means stop, like done or clear. The pattern: ask once before the loop, test for the sentinel at the top, and ask again at the bottom of the body. That way the sentinel is never counted or added as if it were real data. In a while True: loop, check for the sentinel straight after asking and leave with break, before the entry is counted or turned into a number.

Try it

main.py

When it asks, try typing "pizza", "curry", "done".

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks the sentinel value gets counted or added like a real reading.
  • Thinks asking for input once before a loop is enough, and the loop will ask again by itself.

Where it comes up