Unit 3 Β· Lesson 1

while Loops

A while loop keeps running as long as its condition is True. It's the "keep going until" loop β€” perfect for countdowns, retries, and anything where you don't know in advance how many times you'll repeat.

Write a basic while loop
Use a loop variable to control repetition
Avoid infinite loops
Apply the countdown and accumulator patterns

The while Loop

The simplest loop in Python β€” check a condition, run the body, repeat:

count = 5 while count > 0: print(count) count = count - 1 print("Liftoff!")

Output: 5 4 3 2 1 Liftoff! (each on its own line).

Here's what happens step by step:

Step 1: Check condition: 5 > 0? True β†’ run body
Step 2: Print 5, then count becomes 4
Step 3: Check condition: 4 > 0? True β†’ run body
…and so on until…
Step 10: Check condition: 0 > 0? False β†’ skip body, continue after loop

The Loop Variable

The variable count is the loop variable. It must change inside the loop β€” otherwise the condition never becomes False and you get an infinite loop!

⚠️ Infinite loop alert: If you forget count = count - 1, the condition count > 0 stays True forever and your program hangs. Always make sure something inside the loop moves you toward the exit condition.

The Accumulator Pattern

A common use of while: build up a total by adding to it each iteration:

total = 0 n = 1 while n <= 5: total = total + n # same as: total += n n = n + 1 print(total) # 15 (1+2+3+4+5)

The shorthand += is very common in loops: total += n means the same as total = total + n. Similarly, count -= 1 means count = count - 1.

Practice Time

Challenges

πŸš€

Challenge 1: Countdown

Guided

Write a while loop that counts down from start to 1, printing each number, then prints "Liftoff!".

Instructions: Using a while loop, print each number from start down to 1 (one per line), then print "Liftoff!" after the loop. Don't print 0.
Hint 1: Start with count = start, then while count > 0:
Hint 2: Inside the loop: print(count) then count -= 1
Hint 3: Full solution: count = start / while count > 0: / print(count) / count -= 1
🎯

Challenge 2: Sum to Target

Guided

Use a while loop to add 1 + 2 + 3 + ... until the total reaches or exceeds the target.

Instructions: Starting from n = 1, keep adding n to total and incrementing n. Stop when total >= target. After the loop, total should hold the accumulated sum and n should be one more than the last number added.
Hint 1: while total < target: β€” keep looping as long as we haven't hit the target
Hint 2: Inside: total += n then n += 1
Hint 3: For target=20: 1+2+3+4+5+6 = 21 β‰₯ 20, so total=21, n=7
πŸ”’

Challenge 3: Digit Summer

Solo

Calculate the sum of digits in a number using a while loop with % and //.

Instructions: Given number, find the sum of its digits and store it in digit_sum. Use a while loop: extract the last digit with % 10, add it to your sum, then remove it with // 10. Loop until the number becomes 0.

Example: 1234 β†’ 1+2+3+4 = 10
Hint 1: temp % 10 gives the last digit. temp // 10 removes it.
Hint 2: while temp > 0: then digit_sum += temp % 10 then temp //= 10
Hint 3: Trace it: 1234 β†’ digit 4, temp 123 β†’ digit 3, temp 12 β†’ digit 2, temp 1 β†’ digit 1, temp 0 β†’ stop. Sum = 10.
πŸŒ€

Challenge 4: Collatz Sequence

Stretch

The Collatz Conjecture: start with any positive integer. If even, divide by 2. If odd, multiply by 3 and add 1. Repeat until you reach 1.

Instructions: Given n, apply the Collatz rules in a while loop until n becomes 1. Count the number of steps in steps. Print each value of n as you go.

Example: n=6 β†’ 6, 3, 10, 5, 16, 8, 4, 2, 1 (8 steps)
Hint 1: while n != 1: β€” keep going until we hit 1
Hint 2: Inside: if n is even (n % 2 == 0), set n = n // 2. If odd, set n = n * 3 + 1. Don't forget steps += 1!
Hint 3: Print n after each change: print(n, end=" β†’ ") (or similar).