While loops and repetition
Chapter 33 of 132 · Level 2 · Core Python · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
while repeats while its condition is true. Update the state inside the loop, otherwise the condition may never become false.
Learning goal: While loops and repetition. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- Loop conditions and termination
- Counters sentinels and accumulators
- break continue and else
- Preventing infinite loops
03 · Pinpoint: read the code line by line
- Line 1
remaining = 3Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 2
while remaining > 0:Control the next step. Notice what values enter this statement and which names it affects. - Line 3
print('Try', remaining)Display a result. Notice what values enter this statement and which names it affects. - Line 4
remaining -= 1Create, update, or use a value. Notice what values enter this statement and which names it affects. - Line 5
print('Finished')Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
While loops and repetition: working demonstration
Python · runnableremaining = 3
while remaining > 0:
print('Try', remaining)
remaining -= 1
print('Finished')
Try 3 Try 2 Try 1 Finished
Why it works: while repeats while its condition is true. Update the state inside the loop, otherwise the condition may never become false.
Worked example 2 · While Loops And Repetition in practice
Python · executed and checkedbalance = 5
while balance > 0:
balance -= 2
print('Left:', max(balance, 0))
Left: 3 Left: 1 Left: 0
Pinpoint learning: This practical while loops and repetition demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: balance = 5— Store or update the value on the left using the expression on the right.2: while balance > 0:— Repeat the indented block for each item or while the condition remains true.3: balance -= 2— Store or update the value on the left using the expression on the right.4: print('Left:', max(balance, 0))— Display the value so you can compare it with the verified output.
Worked example 3 · Change one input and compare
Python · executed and checkedbalance = 7
while balance > 0:
balance -= 2
print('Left:', max(balance, 0))Left: 5 Left: 3 Left: 1 Left: 0
Pinpoint learning: Exactly one literal input was changed from the preceding demonstration. Compare the outputs: identify the modified value, trace the operation, and explain which result changed. The displayed output was produced by executing this variant.
Understand the code line by line
1: balance = 7— Store or update the value on the left using the expression on the right.2: while balance > 0:— Repeat the indented block for each item or while the condition remains true.3: balance -= 2— Store or update the value on the left using the expression on the right.4: print('Left:', max(balance, 0))— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Replace the counter with five attempts; explain what would cause an infinite loop.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
while repeats while its condition is true. Update the state inside the loop, otherwise the condition may never become false.
Try 3 Try 2 Try 1 Finished
06 · Build, verify and advance
Real-world task: Create a limited-attempt password practice simulator.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
