Search⌘ K
AI Features

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

  1. Inside the while True: loop, read the user’s input into msg.

  2. If the user types "bye":

    1. print a goodbye message

    2. exit the loop using break

  3. Add at least two more keyword checks using elif.

    1. Pick keywords like "music", "weather", "robot", "food", etc.

    2. When a keyword is found inside the message, print a matching response.

  4. If none of the conditions match, use else to print a general reply.

Example behaviors

  • User types: bye → ChatPy says goodbye and stops

  • User types: I like music → ChatPy replies with your music response

  • User 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()
Adding custom responses to make the chatbot more fun and engaging