始める無料で始める

EchoBot II

respond() 関数を作成したので、次は send_message() という関数を定義しましょう。この関数は message という単一のパラメーターを受け取り、message とボットの応答をログに記録します。

この演習はコースの一部です

Python でチャットボットを作る

コースを見る

演習の手順

  • user_template 文字列の .format() メソッドを使って、ユーザーの message をユーザーテンプレートに組み込み、結果を出力してください。
  • respond() 関数に渡された message を引数として呼び出し、結果を response として保存してください。
  • bot_template 文字列の .format() メソッドを使って、ボットの response をログに記録してください。
  • ボットに "hello" というメッセージを送ってください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Create templates
bot_template = "BOT : {0}"
user_template = "USER : {0}"

# Define a function that sends a message to the bot: send_message
def ____(____):
    # Print user_template including the user_message
    print(____.format(____))
    # Get the bot's response to the message
    response = ____(____)
    # Print the bot template including the bot's response.
    print(____.format(____))

# Send a message to the bot
send_message("____")
コードを編集して実行