Python Simple Chatbot Example

This beginner-friendly example shows how to build a very simple chatbot in Python.

It is a small terminal program that:

  • reads text from the user
  • checks that text with if, elif, and else
  • prints a fixed reply
  • keeps running in a loop until the user types bye

This project is useful for practicing basic Python skills in one place.

Important: this is not an AI chatbot. It is a rule-based chatbot, which means it only responds to messages you have programmed.

What this example builds

This example creates:

  • A text-based chatbot that runs in the terminal
  • A bot that reads user input and replies with fixed answers
  • A small project for practicing beginner Python basics

The chatbot does not think or learn. It just compares the user's message to known text and returns a matching response.

What you will learn

By building this chatbot, you will practice:

  • How to use input() to get text from the user
  • How to use if, elif, and else to choose a reply
  • How to use a while loop to keep the chatbot running
  • How to stop the program with break
  • How lower() and strip() make input easier to match

Basic chatbot code walkthrough

Here is the full minimal chatbot script:

print("Simple Chatbot")
print("Type 'bye' to stop.")

while True:
    message = input("You: ").lower().strip()

    if message == "hello":
        print("Bot: Hi!")
    elif message == "how are you":
        print("Bot: I am doing well.")
    elif message == "bye":
        print("Bot: Goodbye!")
        break
    else:
        print("Bot: Sorry, I don't understand.")

How this code works

1. The program starts with two messages

print("Simple Chatbot")
print("Type 'bye' to stop.")

These lines show a title and tell the user how to exit the chatbot.

2. The while True loop keeps the chatbot running

while True:

A while True loop runs forever unless something stops it.

In this chatbot, the loop keeps asking for messages until the user types bye.

If you are new to loops, see Python while loops explained.

3. The chatbot reads user input

message = input("You: ").lower().strip()

This line does three things:

  • input("You: ") waits for the user to type something
  • .lower() changes the text to lowercase
  • .strip() removes extra spaces at the beginning and end

This helps the chatbot match input more easily.

For example, these all become "hello":

  • hello
  • Hello
  • HELLO
  • hello

If you want more practice with user input, read how to get user input in Python.

4. The if and elif statements choose the reply

if message == "hello":
    print("Bot: Hi!")
elif message == "how are you":
    print("Bot: I am doing well.")
elif message == "bye":
    print("Bot: Goodbye!")
    break
else:
    print("Bot: Sorry, I don't understand.")

This is the chatbot's decision logic:

  • If the message is "hello", the bot says Hi!
  • If the message is "how are you", the bot says I am doing well.
  • If the message is "bye", the bot says goodbye and stops
  • If nothing matches, the else block runs

If you need a refresher, see Python if statements explained.

5. The break statement ends the loop

break

break immediately stops the while True loop.

Without it, the chatbot would keep running forever.

Expected output

Here is one example conversation:

Simple Chatbot
Type 'bye' to stop.
You: hello
Bot: Hi!
You: what is your favorite color
Bot: Sorry, I don't understand.
You: bye
Bot: Goodbye!

This example shows:

  • a known message: hello
  • an unknown message: what is your favorite color
  • the exit message: bye

How the chatbot logic works

The program handles one message at a time.

The logic is simple:

  1. Ask the user for a message
  2. Clean the text with lower().strip()
  3. Compare the message with known phrases
  4. Print the matching response
  5. If no phrase matches, use the fallback reply

This kind of chatbot is called a rule-based chatbot.

It works well for learning because:

  • the logic is easy to follow
  • each response is predictable
  • you can add new replies step by step

Improved version with more responses

Here is a slightly larger version with a few more commands:

print("Simple Chatbot")
print("Type 'bye' to stop.")

while True:
    message = input("You: ").lower().strip()

    if message == "hello":
        print("Bot: Hi!")
    elif message == "how are you":
        print("Bot: I am doing well.")
    elif message == "name":
        print("Bot: I am a simple Python chatbot.")
    elif message == "help":
        print("Bot: Try typing hello, how are you, name, or bye.")
    elif message == "bye":
        print("Bot: Goodbye!")
        break
    else:
        print("Bot: Sorry, I don't understand.")

This version uses the same if/elif structure, so it should still feel familiar.

You can test messages like:

  • hello
  • how are you
  • name
  • help
  • bye

Later, you could move these replies into a dictionary for cleaner code. If you want to learn that next, see Python dictionaries explained.

Common beginner mistakes

Here are some common problems beginners run into with this project.

Forgetting parentheses after input

Wrong:

message = input.lower().strip()

Correct:

message = input("You: ").lower().strip()

input is a function, so it needs parentheses.

Using = instead of == in conditions

Wrong:

if message = "hello":
    print("Bot: Hi!")

Correct:

if message == "hello":
    print("Bot: Hi!")
  • = assigns a value
  • == compares values

Forgetting the colon after if, elif, or else

Wrong:

if message == "hello"
    print("Bot: Hi!")

Correct:

if message == "hello":
    print("Bot: Hi!")

A missing colon causes a syntax error. See how to fix SyntaxError: invalid syntax.

Writing break outside the loop

Wrong:

if message == "bye":
    print("Bot: Goodbye!")

break

Correct:

if message == "bye":
    print("Bot: Goodbye!")
    break

break must be inside a loop.

Not handling uppercase letters or extra spaces

If you do not use .lower().strip(), the chatbot may fail to match input like:

  • Hello
  • HELLO
  • hello

That is why this line is helpful:

message = input("You: ").lower().strip()

Other common causes

You may also run into these problems:

  • The program never exits because there is no break condition
  • The chatbot does not recognize input because the text case does not match
  • A SyntaxError happens because a colon is missing after if, elif, or else
  • An IndentationError happens because blocks are not indented correctly
  • The wrong comparison operator is used in conditions

Ways to extend the project

Once the basic version works, try improving it.

You can:

  • Add more fixed questions and answers
  • Add a help command that lists available inputs
  • Store replies in a dictionary as a next-step refactor
  • Count how many messages the user sends
  • Save a chat log to a file

Here is one small extension idea: count messages.

print("Simple Chatbot")
print("Type 'bye' to stop.")

count = 0

while True:
    message = input("You: ").lower().strip()
    count += 1

    if message == "hello":
        print("Bot: Hi!")
    elif message == "bye":
        print("Bot: Goodbye!")
        print("Bot: You sent", count, "messages.")
        break
    else:
        print("Bot: Sorry, I don't understand.")

This adds one new idea without making the program too complex.

When to move to the next topic

You are ready for the next step when you understand:

  • how input() gets text from the user
  • how if, elif, and else choose different actions
  • how a while True loop repeats the chatbot
  • how break stops the loop

Good next topics are:

  • functions to organize chatbot code better
  • dictionaries for cleaner reply matching
  • file handling if you want to save chat history

A useful next lesson is how to create a simple function in Python.

FAQ

Is this a real AI chatbot?

No. This example is a simple rule-based chatbot that matches user input to fixed replies.

Why use lower() and strip()?

They make input matching easier by removing extra spaces and converting text to lowercase.

Can I add more responses?

Yes. Add more elif blocks or later move the replies into a dictionary.

Why does the chatbot keep running?

It runs inside a while True loop until the user enters the exit word and break stops the loop.

What Python topics should I know first?

Basic input, strings, if statements, and while loops are enough for this project.

See also

Copy the chatbot, run it, and test a few messages.

Then improve it by:

  • adding new replies
  • moving code into functions
  • saving chat history to a file

That is a great way to turn one small example into real Python practice.