Word count dictionary: {'NLP': 2, 'is': 1, 'fun': 1}
Exercise: Manage a Simple Chat Log
Task: Create a simple chat log data structure using a dictionary to store messages between a user and a chatbot.
Instructions:
Define a function called create_chat_log() that initializes an empty chat log dictionary.
Define another function called add_message(chat_log, sender, message) that takes the chat log, a sender (“user” or “chatbot”), and the message as parameters.
The function should append the new message to a list under the appropriate sender in the chat log.
Define a function called get_chat_log(chat_log) that returns the entire chat log.
Call the functions to create a chat log, add messages from both the user and the chatbot, and then print the entire chat log.
Example structure of the chat log:
{"user": ["Hello!", "What is NLP?"],"chatbot": ["Hi there!", "NLP stands for Natural Language Processing."]}<details><summary>Show solution</summary>::: {#cell-7 .cell execution_count=2}``` {.python .cell-code}def create_chat_log():return {"user": [], "chatbot": []}def add_message(chat_log, sender, message):if sender in chat_log: chat_log[sender].append(message)else:print("Sender not recognized. Use 'user' or 'chatbot'.")def get_chat_log(chat_log):return chat_log# Example usagechat_log = create_chat_log()add_message(chat_log, "user", "Hello!")add_message(chat_log, "chatbot", "Hi there!")add_message(chat_log, "user", "What is NLP?")add_message(chat_log, "chatbot", "NLP stands for Natural Language Processing.")# Print the chat logprint("Chat Log:", get_chat_log(chat_log))
Chat Log: {'user': ['Hello!', 'What is NLP?'], 'chatbot': ['Hi there!', 'NLP stands for Natural Language Processing.']}