ChitChat
_________________________________________________________________________________
Problem No 1) Define a respond() function which takes in a message argument, checks if the message has a pre-defined response, and returns the response in the responses dictionary if there is a match, or the "default" message otherwise.
Problem Solution: This problem can be solved with different methods and approaches. Here is one solution.
# Define variables
name = "Greg"
weather = "cloudy"
# Define a dictionary with the predefined responses
responses = {
"what's your name?": "my name is {0}".format(name),
"what's today's weather?": "the weather is {0}".format(weather),
"default": "default message"
}
# Return the matching response if there is one, default otherwise
def respond(message):
# Check if the message is in the responses
if message in responses:
# Return the matching message
bot_message = responses[message]
else:
# Return the "default" message
bot_message = responses["default"]
return bot_message
_________________________________________________________________________________
References:
1) https://campus.datacamp.com/courses/building-chatbots-in-python/chatbots-101?ex=1
Last modified: Friday, 23 December 2022, 11:01 AM