Aan de slagGa gratis aan de slag

Nested Functions I

You've learned in the last video about nesting functions within functions. One reason why you'd like to do this is to avoid writing out the same computations within functions repeatedly. There's nothing new about defining nested functions: you simply define it as you would a regular function with def and embed it inside another function!

In this exercise, inside a function three_shouts(), you will define a nested function inner() that concatenates a string object with !!!. three_shouts() then returns a tuple of three elements, each a string concatenated with !!! using inner(). Go for it!

Deze oefening maakt deel uit van de cursus

Introduction to Functions in Python

Cursus bekijken

Oefeninstructies

  • Complete the function header of the nested function with the function name inner() and a single parameter word.
  • Complete the return value: each element of the tuple should be a call to inner(), passing in the parameters from three_shouts() as arguments to each call.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Define three_shouts
def three_shouts(word1, word2, word3):
    """Returns a tuple of strings
    concatenated with '!!!'."""

    # Define inner
    def ____(____):
        """Returns a string concatenated with '!!!'."""
        return word + '!!!'

    # Return a tuple of strings
    return (____, ____, ____)

# Call three_shouts() and print
print(three_shouts('a', 'b', 'c'))
Code bewerken en uitvoeren