Search⌘ K
AI Features

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

  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