Challenge: Custom Responses
Explore building a simple Python chatbot called ChatPy that interacts with users by responding differently to messages based on keywords. Learn to implement a continuous input loop, handle exit conditions, and provide custom replies, strengthening your understanding of if, elif, and else statements in real programming scenarios.
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()