Have you ever chatted with a bot on a shopping site or a railway enquiry page? You type a question and an answer pops up in a second. It can feel like magic. In this project you will build your own small chatbot called Chotu and discover the simple trick behind many basic bots.
You only need Python and about 20 minutes.
How does a simple chatbot work?
There are two main kinds of chatbots.
Rule-based chatbots look for certain words in your message and reply with an answer that a person prepared earlier. If you type "track my order", the bot spots the word "order" and shows the order-tracking reply. Many customer-help bots work this way.
AI chatbots are very different on the inside. They have learned patterns from enormous amounts of text and generate a brand-new reply word by word. They are much more flexible, but they can also make mistakes that sound convincing.
Today we build a rule-based bot. It is the perfect first step, because you can see every single decision it makes. Nothing is hidden.
The plan for Chotu
Chotu needs three things:
- A list of keywords and replies. We store these in a Python dictionary. A dictionary pairs a key with a value, like a word and its meaning in a real dictionary. Here the key is a keyword such as
"cricket"and the value is Chotu's reply. - A function that picks a reply. A function is a named block of code you can reuse. Our function
chotu_replytakes a message, checks it for each keyword, and returns the matching reply. - A fallback. If no keyword matches, Chotu should say something polite instead of staying silent.
One small trick makes Chotu smarter: we change every message to lowercase with .lower() first. That way "CRICKET", "Cricket" and "cricket" are all treated the same.
Try it: build Chotu
- Open Python on your computer or an online Python editor.
- Type in the program below.
- Run it and read the conversation it prints.
- Check your output matches the one shown after the code.
# A tiny rule-based chatbot called Chotu
replies = {
"hello": "Namaste! I am Chotu, a tiny chatbot.",
"name": "My name is Chotu. What is yours?",
"cricket": "Cricket is great! Who is your favourite player?",
"food": "I can't eat, but I hear pani puri is amazing.",
"homework": "Homework first, games later. Deal?",
"bye": "Bye! Come back and chat soon.",
}
def chotu_reply(message):
message = message.lower()
for keyword in replies:
if keyword in message:
return replies[keyword]
return "Hmm, I don't understand that yet. Teach me?"
test_messages = [
"Hello there",
"What is your name?",
"I love CRICKET",
"Do you like food?",
"Can you fly?",
"ok bye",
]
for msg in test_messages:
print("You: ", msg)
print("Chotu:", chotu_reply(msg))
print()
The first part of the output looks like this:
You: Hello there
Chotu: Namaste! I am Chotu, a tiny chatbot.
You: What is your name?
Chotu: My name is Chotu. What is yours?
You: I love CRICKET
Chotu: Cricket is great! Who is your favourite player?
And when you ask "Can you fly?", Chotu answers: Hmm, I don't understand that yet. Teach me?
Make Chotu your own
Here are some upgrades to try, one at a time:
- Teach new words. Add a line to the dictionary, such as
"exam": "You've got this. Revise a little every day!". Then add a test message that uses it. - Chat for real. Replace the
for msg in test_messages:loop with a loop that asks you for input:while True:thenmsg = input("You: "), print Chotu's reply, andbreakwhen the message contains "bye". Now you can type to Chotu live. - Find a bug. Try the message "I renamed my school project". Chotu replies with its name! Can you see why? The word "renamed" has "name" hiding inside it. Testing odd messages like this is how programmers find weak spots.
- Spot the order problem. Send "hello, what is your name?" Chotu replies to "hello" only, because it returns the first match it finds. How could you change that?
For parents
This project quietly teaches three core programming ideas: dictionaries, functions and loops. It also opens a useful conversation: a rule-based bot only "knows" what someone typed into it, while modern AI chatbots generate text from patterns and can be wrong. Ask your child which kind of bot they think they use at school or at home, and why.
What you learned
- A rule-based chatbot looks for keywords and returns replies written in advance.
- A Python dictionary stores pairs of keys and values.
- A function packs code into a reusable block that can
returnan answer. .lower()helps a program treat different capital letters the same way.- AI chatbots generate new replies from learned patterns, which makes them flexible but not always correct.
Keep building
Chotu is a great start, but it cannot understand a message it has no keyword for. In a full course on how computers handle language, students learn to split text into tokens, clean it, turn words into numbers, and train a model that sorts messages on its own. Want to build more? Book a free demo class and see how your child can go from keyword bots to real language AI.