Challenge: Custom Responses
Explore how to build a simple Python chatbot named ChatPy that interacts with users by responding uniquely to different keywords. Learn to use loops, conditional statements, and user input handling to manage conversation flow, including exiting on a goodbye message. This lesson helps you practice creating custom responses and controlling program behavior in real-time.
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()