count = 1
while count <= 3:
print(count)Line-by-line explanation
- Count starts at 1.
- The condition remains True.
- Nothing changes count.
- The loop displays 1 forever. This example is intentionally broken.
for number in range(1, 4):
print(number)Line-by-line explanation
range(1, 4)produces 1, 2, and 3.- The ending value 4 is not included.
- The loop displays exactly three numbers.
If a program is stuck in a terminal, Ctrl + C usually asks it to stop.
Tiny Practice
Fix the loop so it displays 1, 2, and 3, then stops.
Starter template
count = 1
while count <= 3:
print(count)
CHANGE_MELine-by-line explanation
- The condition is correct.
- The counter must change.
Hint
- Assign a new value to count.
- Add 1 to its current value.
- Keep the line inside the loop.
Show Solution
count = 1
while count <= 3:
print(count)
count = count + 1Line-by-line explanation
- Count begins at 1.
- Each iteration displays it.
- The increment eventually makes the condition False.