PYTHON PROJECT · FREE TUTORIAL

Build a Python chatbot without an API key

Create a chatbot that answers a small collection of questions, politely handles unknown inputs, and keeps running until the user exits. It's a genuine beginner Python project, and importantly it is rule-based—not an AI language model.

By Lumio AI · Published October 13, 2026
Rule-based chatbot flow: normalize user input, look up a FAQ dictionary and answer or fall back
Unlike an AI model, this beginner chatbot only returns predefined FAQ answers or a fallback.

Requirements and learning goals

You need Python 3 and a text editor. The first version takes roughly 25–40 minutes for a beginner familiar with dictionaries and loops. You will practice input, normalization, functions, loops, exit commands and tests. No packages, secret keys or online APIs are required.

Step 1: Save a complete working chatbot

Open a file named study_bot.py and paste the entire program:

FAQ = {
    "what is python?": "Python is a programming language.",
    "what is ai?": "AI is a field of computing.",
    "where do i start?": "Start with a small Python program.",
}

def answer(question):
    normalized = " ".join(question.strip().lower().split())
    return FAQ.get(normalized, "I don't know that yet.")

def main():
    print("Study Bot: Ask a question, or type exit.")
    while True:
        user_input = input("You: ")
        if user_input.strip().lower() in {"exit", "quit"}:
            print("Study Bot: See you next time!")
            break
        print("Study Bot:", answer(user_input))

if __name__ == "__main__":
    main()

Run python study_bot.py, or python3 study_bot.py on systems where that is the Python command. Ask what is python?, then exit. You should see the stored answer and then the farewell message.

Step 2: Understand the moving parts

A dictionary is the knowledge base

Each known question maps to one predefined answer. The bot does not generate or learn answers from text.

A normalization function makes exact matching more forgiving

Lowercasing, trimming and collapsing spaces lets users type “ WHAT IS AI? ” and still match “what is ai?”. It does not handle every synonym or different wording.

A loop keeps the conversation open

while True repeatedly accepts questions until break runs. An if __name__ == "__main__" guard means importing answer for tests won't launch the loop.

Step 3: Prove that the answers work

Create test_study_bot.py in the same folder. These tests run without a testing library.

from study_bot import answer

assert answer("what is python?") == "Python is a programming language."
assert answer("  WHAT  IS AI? ") == "AI is a field of computing."
assert answer("something else") == "I don't know that yet."
print("All checks passed.")

Run python test_study_bot.py. The message should appear when all assertions pass. Try intentionally breaking one expected value so you can see what a test failure looks like.

Step 4: Observe the limitations

Ask “Can you explain Python?” even though the dictionary contains “what is python?”. It will return the fallback because the strings do not match after normalization. The program cannot reason, retrieve fresh knowledge, fact-check, or hold a meaningful free-form conversation. It does not use a language model.

Step 5: Choose a realistic extension

  1. Make several question phrasings point to the same stored answer.
  2. Use a JSON file for your FAQ database, using Python's built-in JSON module.
  3. Write more unit tests using unittest.
  4. Let users choose “list questions” to see the exact supported questions.

From this project to real AI applications

Need a refresher before tackling this chatbot? Review while loops and stopping conditions and Python functions with return values; both skills appear in the code above.

A future LLM-powered version could use a language model API, but introduces costs and engineering concerns: authentication, API-key safety, input limits, evaluation, hallucinations and user privacy. Never embed a paid secret API key in browser-side JavaScript or a publicly distributed app. Read the AI engineering roadmap for the next skills.

A good portfolio entry describes what it actually does, includes runnable instructions and tests, and documents failure cases. For additional practice, complete the ten beginner Python exercises. Lumio's guided missions are planned rather than currently available to everyone. Continue learning with free seven-day Python-for-AI practice plan.