🐍 Python for Everyone

← Back to exercises

Chapter 5 Practice Bank 35-45 min, hands-on

Practice Bank: Loops (while & for)

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Loops are one of the most common interview warm-up areas, so several of these lean directly into that. Still uses only while loops, for loops with range(), the accumulator pattern, break/continue, and everything from earlier chapters.

How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: while loops

1 Count 1 to 5

Find # TODO 1.1. Use a while loop to print the numbers 1 through 5, one per line.

2 Countdown

Find # TODO 1.2. Use a while loop to count down from 10 to 1, one number per line.

3 Evens by stepping

Find # TODO 1.3. Use a while loop to print every even number from 2 to 20 (inclusive), stepping by 2 each time.

4 Doubling until 100

Find # TODO 1.4. Start a variable at 1 and keep doubling it (value = value * 2) and printing it, for as long as it's <= 100.

5 Debug the Code

Find # TODO 1.5. This while loop never updates its loop variable — running it as-is would loop forever, so it's commented out. Uncomment it, add the missing step, and confirm it prints 1 through 5 and stops.

💼 Scenario

Launch sequence

Find # TODO 1.A. Mission control needs a countdown script: count down from 5 to 1 with a while loop, then print "Liftoff! 🚀" after the loop ends.

💼 Interview Prep

Why while True: never stops

Find # TODO 1.B. Explain in a comment why while True: with no break and no changing condition never stops on its own, then write a safe, bounded while loop counting 1 to 3 as contrast.

Topic 2: for loops and range()

1 Count 1 to 5

Find # TODO 2.1. Use a for loop with range() to print the numbers 1 through 5.

2 Multiples of 5

Find # TODO 2.2. Use range()'s start/stop/step form to print every multiple of 5 from 0 to 45 (inclusive).

3 Countdown with a negative step

Find # TODO 2.3. Use range()'s negative step form to count down from 10 to 1.

4 Even or odd

Find # TODO 2.4. Loop over range(1, 21) and print whether each number is "even" or "odd" using if/else and %.

5 Debug the Code

Find # TODO 2.5. This loop is supposed to print 1 through 10 inclusive, but stops at 9 because of an off-by-one range() bound. Fix it.

💼 Scenario

Multiplication table

Find # TODO 2.A. Your manager wants a quick reference sheet for order quantities of 12 units. Print the multiplication table of 12, from 12 x 1 to 12 x 10.

💼 Interview Prep

Even numbers, the efficient way

Find # TODO 2.B. Print only the even numbers from 1 to 20 — but without checking each number with % 2. Use range()'s step argument instead.

Topic 3: Common loop patterns

1 Sum with a while loop

Find # TODO 3.1. Use a while loop and the accumulator pattern to sum the numbers 1 through 10.

2 Sum with a for loop

Find # TODO 3.2. Use a for loop and the accumulator pattern to sum the numbers 1 through 100.

3 Count accumulator

Find # TODO 3.3. Count how many numbers from 1 to 50 are evenly divisible by 3, using a count accumulator.

4 Debug the Code

Find # TODO 3.4. This accumulator gets reset to 0 inside the loop on every iteration instead of once before it starts, so the final total is wrong. Fix it.

5 Product accumulator (factorial)

Find # TODO 3.5. Use a product accumulator (started at 1, not 0!) to compute 5 factorial.

💼 Scenario

Daily transactions

Find # TODO 3.A. Three transactions came in today: $19.99, $45.50, and $12.25. Using a while loop and an if/elif chain to pick the right amount each time, add each one to a running total.

💼 Interview Prep

Palindrome check

Find # TODO 3.B. A classic interview warm-up: given n = 1221, use a while loop and the digit-peeling pattern (% and //) to reverse the number, then check if it's a palindrome.

Topic 4: break & continue

1 Break early

Find # TODO 4.1. Loop over range(1, 21) and break the moment the loop variable reaches 15.

2 Skip with continue

Find # TODO 4.2. Loop over range(1, 11) and use continue to skip printing any multiple of 3.

3 Break and continue together

Find # TODO 4.3. Loop over range(1, 31), break once the number exceeds 20, and continue to skip odd numbers — so only even numbers up to 20 print.

4 Debug the Code

Find # TODO 4.4. This loop should skip multiples of 4 with continue, but it uses break instead, so it stops completely at the first one. Fix it.

5 Search and break

Find # TODO 4.5. Use a while loop with break to find the first number greater than 100 that's evenly divisible by both 7 and 3.

💼 Interview Prep

Retry attempt simulator

Find # TODO 4.A. A login system allows at most 3 attempts. Simulate 3 hard-coded attempts against a correct code, using a while loop with break once the code matches, and print "Account locked" if all attempts fail.

💼 Scenario

Sensor data cleanup

Find # TODO 4.B. A weather station logs 5 readings, some negative (sensor glitches). Use continue to skip the invalid negative readings while summing only the valid ones.

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Interview Questions →