Every project / Techy

🤖 ChatBot Buddy

Build a chatbot from rules: it remembers your name, does math, tells jokes and talks back.

ChatBot Buddy running
Language
Python
Level
Beginner
Category
Techy
▶ Try it & remix

What you'll learn

  • Regular expressions
  • Dictionaries as memory
  • Functions as data
  • Safe math with ast
  • How early chatbots worked

How it works

  • The rules in brain.py pair a regular expression with a function. The first rule that matches gives the answer.
  • memory is a dictionary the bot fills in, like your name and your favorites, as you chat.
  • Math is read with Python's ast module instead of eval(), so typed-in code can't run.
  • reflect() swaps words like 'I' and 'you', so the bot can echo what you said back to you.

Knobs to turn

These settings are near the top of the code. Remix it, change one, and watch what happens. The preview updates as you type (press ▶ Run for Python).

SettingStarts atFileWhat it does
BOT_NAME"Byte"brain.pywhat the bot calls itself

Files

FileLines
main.py16where the program starts
brain.py155The bot's brain: a list of rules. Each rule is a pattern to look for and a function that

Challenges

Each one is a bit harder than the last. Teachers: these make good assignment instructions.

  1. Give the bot a name and a personality: change its replies.
  2. Add 5 jokes of your own.
  3. Teach it to remember your age, then tell you how old you'll be in 10 years.
  4. Add a quiz mode where the bot asks YOU questions.
  5. Make it remember everything you say, then answer "what did I say about ___?"