Case Study: Building a Simple Chatbot with Conditionals

The Scenario

You've probably interacted with chatbots — those automated text interfaces that answer questions on customer support websites, guide you through troubleshooting steps, or help you order food. Modern chatbots use sophisticated natural language processing and machine learning, but at their core, every chatbot makes decisions: "What did the user say? What should I respond with?"

In this case study, you'll build a simple rule-based chatbot using only the conditional logic from Chapter 4. It won't understand natural language — it will match keywords and route responses accordingly. It's crude by modern standards, but it demonstrates the exact same decision-making architecture that underlies even the most advanced systems.

The Design

Our chatbot, PyBot, will handle a university help desk scenario. It can respond to questions about: - Office hours - Grading policies - Assignment deadlines - Technical support - General greetings

The decision logic works like this: examine the user's input for keywords, and respond based on what's found.

The Implementation

"""
PyBot — A simple rule-based chatbot using conditionals.
Demonstrates: if-elif-else, Boolean operators, string methods, match/case.
"""

print("=" * 50)
print("  Welcome to PyBot - CS Department Help Desk")
print("  Type your question below.")
print("=" * 50)
print()

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

# --- Greeting detection ---
is_greeting = (
    "hello" in user_input
    or "hi" in user_input
    or "hey" in user_input
    or "good morning" in user_input
)

# --- Farewell detection ---
is_farewell = (
    "bye" in user_input
    or "goodbye" in user_input
    or "see you" in user_input
    or "thanks" in user_input
)

if is_greeting:
    print("PyBot: Hello! I'm PyBot, the CS department assistant.")
    print("       I can help with office hours, grading, deadlines,")
    print("       or technical issues. What do you need?")

elif is_farewell:
    print("PyBot: Goodbye! Good luck with your studies.")

elif "office hours" in user_input or "office" in user_input:
    print("PyBot: Office hours for CS courses:")
    print("       - CS 101: Mon/Wed 2-4 PM, Room 312")
    print("       - CS 201: Tue/Thu 10 AM-12 PM, Room 415")
    print("       - CS 301: By appointment — email the professor.")
    print()

    # Nested conditional for follow-up
    if "cs 101" in user_input or "101" in user_input:
        print("       CS 101 is taught by Prof. Martinez.")
        print("       Drop-ins welcome during office hours.")
    elif "cs 201" in user_input or "201" in user_input:
        print("       CS 201 is taught by Prof. Chen.")
        print("       Please sign up on the office hours sheet.")

elif "grade" in user_input or "grading" in user_input:
    print("PyBot: Grading information:")

    if "late" in user_input or "penalty" in user_input:
        print("       Late work policy: 10% penalty per day,")
        print("       up to 3 days maximum. After 3 days, no credit.")
    elif "curve" in user_input:
        print("       Grading curves vary by course.")
        print("       Check your course syllabus for details.")
    elif "weight" in user_input or "breakdown" in user_input:
        print("       Typical CS course breakdown:")
        print("       - Assignments: 40%")
        print("       - Midterm: 25%")
        print("       - Final: 25%")
        print("       - Participation: 10%")
    else:
        print("       Could you be more specific?")
        print("       I can help with: late penalties, grading curves,")
        print("       or grade weight breakdowns.")

elif "deadline" in user_input or "due" in user_input or "assignment" in user_input:
    print("PyBot: Upcoming deadlines:")
    print("       - CS 101 HW 4: Friday, Oct 11")
    print("       - CS 201 Project 2: Monday, Oct 14")
    print("       - CS 301 Paper: Wednesday, Oct 16")

elif "help" in user_input or "tech" in user_input or "error" in user_input or "bug" in user_input:
    print("PyBot: For technical support:")
    print()

    if "python" in user_input or "install" in user_input:
        print("       Python installation issues:")
        print("       1. Download Python 3.12+ from python.org")
        print("       2. Check 'Add to PATH' during installation")
        print("       3. Verify: open terminal, type 'python --version'")
    elif "vscode" in user_input or "editor" in user_input:
        print("       VS Code issues:")
        print("       1. Install the Python extension by Microsoft")
        print("       2. Select the correct Python interpreter (Ctrl+Shift+P)")
        print("       3. Restart VS Code after installing extensions")
    else:
        print("       General troubleshooting steps:")
        print("       1. Read the error message carefully")
        print("       2. Check for typos in your code")
        print("       3. Search the error message online")
        print("       4. Visit the tutoring center (Room 200)")

else:
    print("PyBot: I'm not sure I understand. I can help with:")
    print("       - Office hours")
    print("       - Grading policies")
    print("       - Assignment deadlines")
    print("       - Technical support")
    print("       Try asking about one of those topics!")

Sample Interactions

Interaction 1 — Greeting:

You: Hey there
PyBot: Hello! I'm PyBot, the CS department assistant.
       I can help with office hours, grading, deadlines,
       or technical issues. What do you need?

Interaction 2 — Specific grade query:

You: What's the late penalty for grading?
PyBot: Grading information:
       Late work policy: 10% penalty per day,
       up to 3 days maximum. After 3 days, no credit.

Interaction 3 — Technical help with Python:

You: I need help with a Python installation error
PyBot: For technical support:

       Python installation issues:
       1. Download Python 3.12+ from python.org
       2. Check 'Add to PATH' during installation
       3. Verify: open terminal, type 'python --version'

Interaction 4 — Unknown topic:

You: What's the meaning of life?
PyBot: I'm not sure I understand. I can help with:
       - Office hours
       - Grading policies
       - Assignment deadlines
       - Technical support
       Try asking about one of those topics!

Analysis: What Makes This Work (and What Doesn't)

The Decision Architecture

PyBot uses a two-level decision tree:

  1. Level 1 (topic detection): The outer if-elif-else chain identifies the broad topic — greeting, farewell, office hours, grading, deadlines, or tech support.

  2. Level 2 (sub-topic refinement): Nested conditionals within some branches narrow down the response. For grading, it checks for "late," "curve," or "weight" to give a targeted answer.

This is the same architecture used by early commercial chatbots (often called "decision tree bots" or "rule-based bots"). The branching structure maps directly to the flowcharts we discussed in Section 4.2.

Limitations

This chatbot has significant limitations that illustrate why simple conditionals aren't enough for real conversational AI:

  1. Keyword fragility: The user must use the exact keywords the bot expects. "When can I meet the prof?" won't match "office hours."

  2. No context: Each interaction is independent. The bot can't remember what you asked before. (We'll learn about state and loops in Chapter 5.)

  3. Order sensitivity: If the user types "What are the office hours for grading TAs?", the bot matches "office" first and never checks for "grading."

  4. No spelling tolerance: "gradeing" or "deadlien" won't match anything.

  5. Single interaction: The bot handles one question and exits. A real chatbot would loop, maintaining a conversation.

What Real Chatbots Add

Modern chatbots build on this conditional foundation but add layers: - Natural language processing (NLP): Understands intent regardless of exact wording - Machine learning: Learns from past conversations to improve - State management: Tracks the conversation context across multiple exchanges - Fuzzy matching: Handles typos and synonyms

But strip away those layers, and at the bottom you'll still find decision logic — just like the if-elif-else chains in PyBot.

Discussion Questions

  1. The chatbot uses .lower() on the user's input. What would happen if it didn't? Give a specific example of an input that would fail without .lower().

  2. Why does the code use "office hours" in user_input rather than user_input == "office hours"? What's the practical difference?

  3. If you added a new topic — "financial aid" — where would you insert it in the if-elif-else chain? Does position matter? Why or why not?

  4. The code stores is_greeting and is_farewell as Boolean variables before the if chain. What's the advantage of this approach over putting all the or conditions directly in the if statement?

  5. How would you modify PyBot to handle the case where a user's input matches multiple topics (e.g., "What's the deadline for the grading policy update?")? What trade-offs are involved?

Extension Challenge

Modify PyBot to use match/case for the top-level routing. You'll need to redesign the architecture slightly — instead of checking for keywords in free text, have the user choose a numbered menu option first, then ask follow-up questions. Compare the readability of both approaches.