Every project / Techy
🤖 ChatBot Buddy
Build a chatbot from rules: it remembers your name, does math, tells jokes and talks back.
- Language
- Python
- Level
- Beginner
- Category
- Techy
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).
| Setting | Starts at | File | What it does |
|---|---|---|---|
BOT_NAME | "Byte" | brain.py | what the bot calls itself |
Files
| File | Lines | |
|---|---|---|
main.py | 16 | where the program starts |
brain.py | 155 | The 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.
- Give the bot a name and a personality: change its replies.
- Add 5 jokes of your own.
- Teach it to remember your age, then tell you how old you'll be in 10 years.
- Add a quiz mode where the bot asks YOU questions.
- Make it remember everything you say, then answer "what did I say about ___?"