Module 7 – Chatbots & AI Helpers
intermediate30 XP

Build Your Own Q&A Bot in Python! πŸ› οΈ

Write a real working chatbot in Python using dictionaries and if-elif logic

Build Your Own Q&A Bot in Python! πŸ› οΈ

Your First Chatbot!

Let's build a simple FAQ chatbot using Python. This is how early customer service bots worked!

Version 1: Dictionary Bot πŸ“š

Python
# A dictionary maps questions to answers
knowledge_base = {
    "hello": "Hi there! I'm RoboBot πŸ€– How can I help?",
    "what is ai": "AI stands for Artificial Intelligence β€” computers that can learn!",
    "who made you": "I was made by a coding student using Python!",
    "what can you do": "I can answer questions, tell jokes, and be your friend!",
    "tell me a joke": "Why don't scientists trust atoms? Because they make up everything! πŸ˜‚",
    "bye": "Goodbye! Keep coding! πŸ‘‹",
}

# Chat loop
print("RoboBot: Hi! I'm RoboBot. Type 'bye' to quit!")
while True:
    user_input = input("You: ").lower().strip()
    
    if user_input in knowledge_base:
        print(f"RoboBot: {knowledge_base[user_input]}")
    else:
        print("RoboBot: Hmm, I don't know that one yet! Try asking something else.")
    
    if user_input == "bye":
        break

Version 2: Smarter Keyword Detection 🧠

Python
def get_response(user_input):
    user_input = user_input.lower()
    
    # Check for keywords anywhere in the sentence
    if "hello" in user_input or "hi" in user_input:
        return "Hey there! Great to meet you! πŸ‘‹"
    
    elif "joke" in user_input or "funny" in user_input:
        return "Why did the computer go to the doctor? Because it had a virus! πŸ€’πŸ’»"
    
    elif "how are you" in user_input or "you ok" in user_input:
        return "I'm running at 100% efficiency! No bugs today! πŸ›βœ…"
    
    elif "your name" in user_input or "who are you" in user_input:
        return "I'm RoboBot 3000! World's friendliest chatbot! πŸ€–"
    
    elif "bye" in user_input or "goodbye" in user_input:
        return "See you later, human! πŸ‘‹ Keep learning!"
    
    else:
        return "Interesting question! I'm still learning. Try asking about jokes or how I'm doing! πŸ€”"

# Run it!
print("RoboBot 3000 is online! πŸ€–")
while True:
    message = input("You: ")
    response = get_response(message)
    print(f"RoboBot: {response}")
    if "bye" in message.lower():
        break

πŸ† Challenge: Upgrade Your Bot!

Add to your chatbot:

  1. A favourite colour question (ask what theirs is!)
  2. A maths helper (detect if they ask a maths question)
  3. A fun fact response when they say "fact"
  4. Your own personalised bot name and personality!
⌨️

Type it yourself

Don't just read it β€” type the example from above into the box, press Run β–Ά, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.

Python Β· Playground
Output
Press β–Ά Run to see your code come to life…

Quick check

01/2

In the dictionary chatbot, what happens when the user asks something not in the knowledge_base?