Book-matched resources · Chapter 03 · Challenges 021–030

Loops and Repetition

Try the printed challenge before revealing a hint or the example solution. Starter files are available only where the book supplies one.

Challenge hints & worked solutions

CHALLENGE 021 · BUILD IT

Rocket Countdown

The launch crew needs a countdown program. Ask where the countdown should begin, display every whole number down to zero, and finish with Launch! Assume the start value is zero or greater.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

A three-argument range() can count backwards. A stop value of -1 is needed to include zero.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 21 - Rocket Countdown
start = int(input("Countdown starts at: "))
for number in range(start, -1, -1):
    print(number)
print("Launch!")
Save this Python file ↓
CHALLENGE 022 · BUILD IT

Times-Table Trainer

Create a times-table generator that can display the table for any whole number, from multiplication by 1 through multiplication by 12.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

Loop through multipliers 1 to 12. Print a formatted string using the chosen number and loop variable.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 22 - Times-Table Trainer
number = int(input("Which times table? "))
for multiplier in range(1, 13):
    print(f"{number} x {multiplier} = {number * multiplier}")
Save this Python file ↓
CHALLENGE 023 · BUILD IT

Daily Steps Total

A health challenge asks volunteers to record their step count for seven days. Ask for a count each day and calculate the week’s total. Assume the entered counts are valid, non-negative integers.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

Set total to zero before the loop; add steps to that total in each iteration.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 23 - Daily Steps Total
total = 0
for day in range(1, 8):
    steps = int(input(f"Steps on day {day}: "))
    total += steps
print(f"Total steps: {total}")
Save this Python file ↓
CHALLENGE 024 · IMPROVE IT

Please Enter a Name

A welcome screen currently asks for a name only once. If the learner presses Enter without entering a name, it gives up. Improve the supplied program so it keeps asking until a name is entered.

Reveal a hint (from the printed book)

Normalise input with .strip(). As long as the result is empty, ask again.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 24 - Please Enter a Name
name = input("Your name: ").strip()
while not name:
    print("Name cannot be blank.")
    name = input("Your name: ").strip()
print(f"Welcome, {name}!")
Save this Python file ↓
CHALLENGE 025 · BUILD IT

Find the Secret Number

Build a number-guessing game with a fixed secret number of 7. After each incorrect guess, say whether the guess is too low or too high. Repeat until the player gets it right.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

After printing Too low or Too high, ask for a fresh guess inside the while loop.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 25 - Find the Secret Number
SECRET = 7
guess = int(input("Guess the number: "))
while guess != SECRET:
    if guess < SECRET:
        print("Too low")
    else:
        print("Too high")
    guess = int(input("Guess again: "))
print("You found the secret number!")
Save this Python file ↓
CHALLENGE 026 · BUILD IT

Multiples in a Range

Ask for two integer limits and print all multiples of three between them, including any qualifying limits. For this challenge, assume the first limit is no greater than the second.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

Use range(start, end + 1) and test each number with number % 3 == 0.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 26 - Multiples in a Range
start = int(input("First number: "))
end = int(input("Last number: "))
found = False
for number in range(start, end + 1):
    if number % 3 == 0:
        print(number)
        found = True
if not found:
    print("No multiples of 3 found.")
Save this Python file ↓
CHALLENGE 027 · SOLVE IT

Make a Text Pattern

Create a growing triangle using # symbols. The user chooses a positive height. Use two nested loops to construct each row rather than hard-coding a different print command for each line.

Start from a blank Python file; no starter supplied for this challenge.Download worked solution .py ↓
Reveal a hint (from the printed book)

The outer loop controls rows. Build the line by adding one # in each iteration of the inner loop.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 27 - Make a Text Pattern
height = int(input("Triangle height: "))
for row in range(1, height + 1):
    line = ""
    for column in range(row):
        line += "#"
    print(line)
Save this Python file ↓
CHALLENGE 028 · DEBUG IT

Stop the Endless Loop

A developer intended to print 1 to 5, then finish. The supplied program never changes its counter, creating an infinite loop. Inspect it first; do not run it unchanged. Fix the loop with the smallest appropriate edit.

Reveal a hint (from the printed book)

The condition depends on counter, so counter must change. Add counter += 1 inside the loop.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 28 - Stop the Endless Loop
counter = 1
while counter <= 5:
    print(counter)
    counter += 1
print("Finished")
Save this Python file ↓
CHALLENGE 029 · IMPROVE IT

Save for a Goal

The starter saves £10 each week until it reaches £100. Improve it into a flexible savings tracker: the user chooses a starting balance and goal, then enters a contribution for each week. Entering zero should stop the tracker safely.

Reveal a hint (from the printed book)

Keep a week counter. Stop if the goal is already met, or if the contribution is zero. Never add a negative contribution.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 29 - Save for a Goal
balance = float(input("Starting balance: "))
goal = float(input("Savings goal: "))
week = 0
if balance >= goal:
    print("Goal already reached (week 0).")
else:
    while balance < goal:
        deposit = float(input("Weekly deposit (0 to stop): "))
        if deposit == 0:
            print(f"Saving paused after {week} weeks.")
            break
        if deposit < 0:
            print("Use a positive amount or 0 to stop.")
            continue
        week += 1
        balance += deposit
        print(f"Week {week}: balance {balance:.2f}")
    if balance >= goal:
        print(f"Goal reached in week {week}!")
Save this Python file ↓
CHALLENGE 030 · DEBUG IT

The Five-Question Quiz

A short arithmetic quiz generates five questions using a loop. The supplied program checks answers correctly but sometimes reports a suspiciously low final score. Identify and repair the score-reset mistake.

Reveal a hint (from the printed book)

The score must be set to zero exactly once, before the first question is asked.

Reveal a worked Python solution

One possible implementation. Check the book's tests and try an extension after comparing.

# Challenge 30 - The Five-Question Quiz
score = 0
for number in range(1, 6):
    answer = input(f"Q{number}: {number} + 2 = ? ").strip()
    if answer == str(number + 2):
        score += 1
print(f"Final score: {score}/5")
Save this Python file ↓