開始使用免費開始

含多個參數的函式

Hugo 在上一支影片說明了在定義函式時使用多個參數。現在你要運用所學,進一步修改 shout() 函式。這裡你會把 shout() 改成可接受兩個引數。你先前撰寫的 shout() 函式部分程式碼如下。

本練習屬於課程

Python 函式入門

檢視課程

練習說明

  • 修改函式標頭,讓它依序接受兩個參數 word1word2
  • 分別將 word1word2'!!!' 串接,並分別指定給 shout1shout2
  • 依序將 shout1shout2 串接在一起,指定給 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)
編輯並執行程式碼