WEB PRACTICE CHAPTER 06

Functions and Reusable Code

Ten additional coding tasks · W051–W060

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 6 code

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

Chapter 6 starters ↓Chapter 6 worked examples ↓
WEB PRACTICE W051

Reusable greeting

Write a function that returns a greeting for a name.

Reveal a hint

Use a parameter and return a string.

Reveal example code (after trying it yourself)
def greet(name):
    return f"Hello, {name}!"

print(greet("Ada"))

One possible solution. Try writing your own first.

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

WEB PRACTICE W052

Convert kilometres

Write a function that converts kilometres to metres.

Reveal a hint

There are 1000 metres in a kilometre.

Reveal example code (after trying it yourself)
def kilometres_to_metres(km):
    return km * 1000

print(kilometres_to_metres(2.5))

One possible solution. Try writing your own first.

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

WEB PRACTICE W053

Rectangle area function

Create a function for a rectangle area calculation.

Reveal a hint

Return width * height.

Reveal example code (after trying it yourself)
def area(width, height):
    return width * height

print(area(7, 3))

One possible solution. Try writing your own first.

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

WEB PRACTICE W054

Discount function

Create a function that applies a percentage discount.

Reveal a hint

Subtract the percentage from 100 before multiplying.

Reveal example code (after trying it yourself)
def price_after_discount(price, percent):
    return price * (100 - percent) / 100

print(f"£{price_after_discount(40, 15):.2f}")

One possible solution. Try writing your own first.

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

WEB PRACTICE W055

Is the number prime?

Write a function to decide whether a positive integer is prime.

Reveal a hint

Test divisors only up to the square root.

Reveal example code (after trying it yourself)
def is_prime(number):
    if number < 2:
        return False
    for divisor in range(2, int(number ** 0.5) + 1):
        if number % divisor == 0:
            return False
    return True

print(is_prime(29))

One possible solution. Try writing your own first.

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

WEB PRACTICE W056

Factorial function

Calculate a factorial using a function and a loop.

Reveal a hint

Multiply the numbers from 1 to n, starting from one.

Reveal example code (after trying it yourself)
def factorial(n):
    total = 1
    for i in range(2, n + 1):
        total *= i
    return total

print(factorial(5))

One possible solution. Try writing your own first.

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

WEB PRACTICE W057

Safe division

Create a function that returns None when the divisor is zero.

Reveal a hint

Check the denominator before dividing.

Reveal example code (after trying it yourself)
def safe_divide(a, b):
    if b == 0:
        return None
    return a / b

print(safe_divide(12, 3))
print(safe_divide(12, 0))

One possible solution. Try writing your own first.

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

WEB PRACTICE W058

Clamp readings

Write a function that keeps a number within a minimum and maximum.

Reveal a hint

Use min() and max() together.

Reveal example code (after trying it yourself)
def clamp(value, low, high):
    return max(low, min(high, value))

print(clamp(105, 0, 100))

One possible solution. Try writing your own first.

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

WEB PRACTICE W059

Format a full name

Create a function that returns a tidy full name from two inputs.

Reveal a hint

strip() removes surrounding spaces; title() adjusts case.

Reveal example code (after trying it yourself)
def full_name(first, last):
    return first.strip().title() + " " + last.strip().title()

print(full_name("  ada", "LOVELACE "))

One possible solution. Try writing your own first.

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

WEB PRACTICE W060

Average function

Write a function that finds the average of a nonempty list.

Reveal a hint

Return sum(values) / len(values).

Reveal example code (after trying it yourself)
def mean(values):
    return sum(values) / len(values)

print(mean([4, 6, 8]))

One possible solution. Try writing your own first.

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