單一參數的函式
恭喜!你已經成功定義並呼叫了你自己的函式!這很不錯。
在前一個練習中,你定義並呼叫了函式 shout(),它會將一個字串與 '!!!' 串接後印出。
現在你要在 shout() 中加入一個「參數」,讓它可以接受並處理任何傳入的字串「引數」。另外請注意,shout(word) 這段用來指定函式名稱與參數的「標頭」部分,稱為函式的「簽章」(signature)。你在實務中可能會遇到這個術語!
本練習屬於課程
Python 函式入門
練習說明
- 在函式標頭中加入參數名稱
word。 - 將
word與'!!!'串接後的結果指定給shout_word。 - 印出
shout_word的值。 - 呼叫
shout()函式,並傳入字串'congratulations'。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define shout with the parameter, word
def shout(____):
"""Print a string with three exclamation marks"""
# Concatenate the strings: shout_word
____ = ____ + '!!!'
# Print shout_word
print(____)
# Call shout with the string 'congratulations'