WEB PRACTICE CHAPTER 03

Loops and Repetition

Ten additional coding tasks · W021–W030

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 3 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 3 starters ↓Chapter 3 worked examples ↓
WEB PRACTICE W021

Counting to ten

Print each integer from 1 through 10.

Reveal a hint

Use range(1, 11).

Reveal example code (after trying it yourself)
for number in range(1, 11):
    print(number)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W022

Countdown

Count down from 5 to 1 and then print Launch.

Reveal a hint

Give range() a negative step.

Reveal example code (after trying it yourself)
for n in range(5, 0, -1):
    print(n)
print("Launch!")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W023

Multiplication table

Print the first 12 multiples of seven.

Reveal a hint

Multiply the loop counter by 7.

Reveal example code (after trying it yourself)
for n in range(1, 13):
    print(f"7 x {n} = {7*n}")

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W024

Total from 1 to 100

Calculate the sum of the integers from 1 through 100.

Reveal a hint

Start an accumulator at zero.

Reveal example code (after trying it yourself)
total = 0
for n in range(1, 101):
    total += n
print("Total:", total)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W025

Count multiples

Count the multiples of 3 between 1 and 30 inclusive.

Reveal a hint

Use the remainder operator in the loop.

Reveal example code (after trying it yourself)
count = 0
for n in range(1, 31):
    if n % 3 == 0:
        count += 1
print("Multiples:", count)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W026

Star staircase

Display five rows of increasing numbers of stars.

Reveal a hint

String multiplication works with a number.

Reveal example code (after trying it yourself)
for n in range(1, 6):
    print("*" * n)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W027

Weekly steps

Add seven daily step counts and display the weekly total.

Reveal a hint

Loop through the list and update a running total.

Reveal example code (after trying it yourself)
steps = [6500, 7900, 6200, 8500, 7100, 9000, 8100]
total = 0
for day in steps:
    total += day
print("Weekly steps:", total)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W028

While-loop target

Double a value starting at 1 until it reaches at least 100.

Reveal a hint

Make sure the loop changes the value each turn.

Reveal example code (after trying it yourself)
value = 1
while value < 100:
    value *= 2
print("Reached:", value)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W029

Squares table

Display the squares of numbers 1–10.

Reveal a hint

n ** 2 squares n.

Reveal example code (after trying it yourself)
for n in range(1, 11):
    print(n, "squared is", n ** 2)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.

WEB PRACTICE W030

Sum of digits

Add the individual digits of the number 5834.

Reveal a hint

Repeatedly take the final digit with % 10.

Reveal example code (after trying it yourself)
number = 5834
total = 0
while number > 0:
    total += number % 10
    number //= 10
print("Digit sum:", total)

One possible solution. Try writing your own first.

Extension: Change the sample values and test at least two different cases.