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":
breakVersion 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:
- A favourite colour question (ask what theirs is!)
- A maths helper (detect if they ask a maths question)
- A fun fact response when they say "fact"
- 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?