Chapters
Python114 chapters

Exercises

Return Values

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Return both the smallest and largest number, and unpack them into low and high.

Python
def min_max(numbers):
    # return both
    pass

# unpack and print
Exercise 2

Rewrite price_for with early returns rather than nested branches.

Python
def price_for(age):
    if age < 16:
        return 0
    else:
        if age >= 65:
            return 5
        else:
            return 12

for age in [10, 30, 70]:
    print(price_for(age))
Exercise 3

Return a function that multiplies by the given factor.

Python
def multiplier(factor):
    # return a function
    pass

triple = multiplier(3)
print(triple(5))