複数のパラメータをもつ関数
前のレクチャーで、Hugo は関数定義における複数パラメータの使い方を説明しました。ここでは学んだことを使って、shout() 関数をさらに改良します。shout() が 2 つの引数を受け取るように変更しましょう。以前に作成した shout() 関数の一部を以下に示します。
この演習はコースの一部です
Pythonの関数入門
演習の手順
- 関数ヘッダーを変更し、
word1、word2の順で 2 つのパラメータを受け取るようにしてください。 word1とword2それぞれに'!!!'を連結し、順にshout1、shout2に代入します。shout1とshout2をこの順番で連結し、new_shoutに代入します。- 文字列
'congratulations'と'you'をこの順番でshout()に渡し、返り値をyellに代入してください。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define shout with parameters word1 and word2
def shout(____, ____):
"""Concatenate strings with three exclamation marks"""
# Concatenate word1 with '!!!': shout1
# Concatenate word2 with '!!!': shout2
# Concatenate shout1 with shout2: new_shout
# Return new_shout
return new_shout
# Pass 'congratulations' and 'you' to shout(): yell
# Print yell
print(yell)