Challenge: Custom Responses
Explore how to make your Python chatbot more engaging by adding multiple elif statements. This lesson guides you to create custom responses for keywords such as music and weather, helping you practice conditional logic and extend program interactivity.
We'll cover the following...
You’re building a simple chatbot called ChatPy.
ChatPy should:
keep asking the user for a message
reply differently based on what the user types
stop when the user types
"bye"
Your task
Inside the
while True:loop, read the user’s input intomsg.If the user types
"bye":print a goodbye message
exit the loop using
break
Add at least two more keyword checks using
elif.Pick keywords like
"music","weather","robot","food", etc.When a keyword is found inside the message, print a matching response.
If none of the conditions match, use
elseto print a general reply.
Example behaviors
User types:
bye→ ChatPy says goodbye and stopsUser types:
I like music→ ChatPy replies with your music responseUser types:
hello→ ChatPy uses the default response
Hint
Use checks like:
if msg == "bye":elif "music" in msg:
def chatbot():
print("Hi! I'm ChatPy. Let's chat. Type 'bye' to exit.")
while True:
msg = input("You: ")
if msg == "bye":
print("ChatPy: See you later!")
break
# Add your code here.
chatbot()