MulaiMulai sekarang secara gratis

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!

Latihan ini adalah bagian dari kursus

Introduction to Functions in Python

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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'))
Edit dan Jalankan Kode