Chapters
Python114 chapters

Exercises

args and kwargs

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

Exercise 1

Write total so it adds up any number of arguments.

Python
# define total
print(total(1, 2))
print(total(1, 2, 3, 4))
print(total())
Exercise 2

Print each keyword argument as key = value.

Python
# define show
show(colour="red", size=10)
Exercise 3

Write a wrapper that passes everything straight through to the given function.

Python
def area(width, height=2):
    return width * height

def call(fn, *args, **kwargs):
    # call fn with whatever was passed
    pass

print(call(area, 3, height=4))