含多個參數的函式
Hugo 在上一支影片說明了在定義函式時使用多個參數。現在你要運用所學,進一步修改 shout() 函式。這裡你會把 shout() 改成可接受兩個引數。你先前撰寫的 shout() 函式部分程式碼如下。
本練習屬於課程
Python 函式入門
練習說明
- 修改函式標頭,讓它依序接受兩個參數
word1和word2。 - 分別將
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)