Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 61

Simple Chatbot in Python

In this lesson: Build a rule-based chatbot in Python step by step — patterns, responses and loop — complete code with output. In Hindi & English.

What we are building

A chatbot talks with the user by reading their messages and replying. The easiest kind is a rule-based chatbot: it looks for keywords and gives a matching answer. It does not learn, but it teaches loops, conditions and string handling — and it is genuinely fun to build.

💡 Rule-based = check for keywords in the message and reply with a fixed answer.

The chat loop

while True:
    msg = input("You: ").lower()
    if msg == "bye":
        print("Bot: Goodbye!")
        break
    print("Bot: I heard you say", msg)
You: hi
Bot: I heard you say hi
You: bye
Bot: Goodbye!

A while True loop keeps the conversation going. We lowercase the input so matching is reliable, and break ends the chat when the user types "bye".

Keyword rules

while True:
    msg = input("You: ").lower()
    if "hello" in msg or "hi" in msg:
        print("Bot: Hello there!")
    elif "name" in msg:
        print("Bot: I am CodeBot.")
    elif "bye" in msg:
        print("Bot: Bye!")
        break
    else:
        print("Bot: Sorry, I did not understand.")
You: what is your name
Bot: I am CodeBot.

We check if keywords like "hello" or "name" appear in the message with in, and reply accordingly. The else handles anything we do not recognise.

Using a dictionary of replies

As rules grow, a dictionary keeps them tidy.

replies = {
    "hello": "Hi there!",
    "name": "I am CodeBot.",
    "help": "Ask me about Python!"
}

msg = input("You: ").lower()
answer = "Sorry, I did not understand."
for keyword in replies:
    if keyword in msg:
        answer = replies[keyword]
        break
print("Bot:", answer)
You: can you help
Bot: Ask me about Python!

The dictionary maps each keyword to a reply. We loop over the keywords, and if one appears in the message, we use its reply. Adding a new rule is just one more dictionary entry.

Making it smarter

  • More keywords. Add entries for common questions and greetings.
  • Multiple matches. Handle messages that contain several keywords.
  • Random replies. Store a list of answers per keyword and pick one at random.
  • Go further. Later you can use NLP libraries or an AI model for real understanding.

Common mistakes

  • Not lowercasing input. "Hello" would not match "hello" — use .lower().
  • Using == instead of in. in checks if a keyword appears anywhere in the message.
  • No exit condition. Without a "bye" break, the loop runs forever.
  • No else fallback. Always handle messages you do not recognise.

Summary

  • A rule-based chatbot replies to keywords in the message.
  • A while True loop keeps the conversation going.
  • Lowercase input and use in to match keywords.
  • A dictionary of keyword-to-reply keeps many rules tidy.
  • Always add an else fallback and a way to exit.

Next, strengthen the logic with Python loops, or organise replies with dictionaries.

Frequently Asked Questions

What is a rule-based chatbot?
A rule-based chatbot replies based on fixed rules — it looks for keywords in the user's message and gives a matching answer. It does not learn; it follows the rules you write. It is the easiest kind of chatbot to build.
How does a simple chatbot understand input?
It does not truly understand — it checks whether certain keywords appear in the message. For example, if the message contains "hello", it replies with a greeting. Lowercasing the input first makes matching reliable.
How do I keep the chatbot running?
Use a while True loop that keeps reading input and replying, until the user types something like "bye" to exit. The loop is what makes it feel like a conversation.
Why lowercase the user input?
Users type in mixed case ("Hello", "HELLO", "hello"). Converting to lowercase with .lower() means your keyword checks work no matter how they type it.
How can I make my chatbot smarter?
Add more keyword rules, use a dictionary to map keywords to replies, handle several keywords per message, or move to a library or AI model. Rule-based is the foundation you build on.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicEmail Spam Detection Your progress: 61 / 66 (92%) · Lesson 62 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free