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.
The chat loop
while True:
msg = input("You: ").lower()
if msg == "bye":
print("Bot: Goodbye!")
break
print("Bot: I heard you say", msg)
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.")
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)
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 ofin.inchecks if a keyword appears anywhere in the message. - No exit condition. Without a "bye" break, the loop runs forever.
- No
elsefallback. Always handle messages you do not recognise.
Summary
- A rule-based chatbot replies to keywords in the message.
- A
while Trueloop keeps the conversation going. - Lowercase input and use
into match keywords. - A dictionary of keyword-to-reply keeps many rules tidy.
- Always add an
elsefallback 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?
How does a simple chatbot understand input?
How do I keep the chatbot running?
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?
.lower() means your keyword checks work no matter how they type it.