Python 2.2The video for this lesson is on its way.
Everything else is ready: study the parts, the key terms and the quiz below.
- Part 11A question with no known count
- Part 22The while loop: condition, body and iteration
- Part 33Tracing the loop, one iteration at a time
- Part 44How many times does the body run?
- Part 55The infinite loop, and the rule that prevents it
- Part 66A sentinel: looping until an empty entry
- Part 77Input validation: asking again until the answer is usable
- Part 88The assignment expression: reading and testing in one line
- Part 99Recap and your challenge
Python 2.2Code walkthrough9 parts
while Loops
Repeat until a condition changes, and avoid infinite loops.
Loading your progress…Next: 2.3 for Loops and range
In this lesson9 parts
- 01A question with no known count
- 02The while loop: condition, body and iteration
- 03Tracing the loop, one iteration at a time
- 04How many times does the body run?
- 05The infinite loop, and the rule that prevents it
- 06A sentinel: looping until an empty entry
- 07Input validation: asking again until the answer is usable
- 08The assignment expression: reading and testing in one line
- 09Recap and your challenge
Key terms
The words this lesson introduces, each in one line. The module’s glossary collects them all.
- assignment expression
name := value: assigns a value and gives that value, inside an expression.while (price := input("Price: ")) != "":reads and tests in one condition. Also called the walrus operator.- infinite loop
- A loop whose condition never becomes False, because nothing in the body changes what it reads.
- iteration
- One run of the loop body.
- loop
- Code that runs its body again and again: a
whilestatement or aforstatement, with its block. - loop body
- The indented block that repeats.
- sentinel
- A value that marks the end of the input, and is not processed, such as an empty entry at a checkout.
Quiz 5 questions
Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.
Practice
Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.
The self-checking notebook for this lesson is Notebook 2 · Loops.
Common mistakes
What you will see when it goes wrong, why it happens, and the fix.
Where it’s used
Where this lesson’s ideas turn up in real work.