Build-It Project: A Chatbot
Time for a project that pulls together the whole phase: a chatbot. It keeps reading what you type and replies based on what it spots in your message. It’s just a while loop (keep chatting), input (read the message), and if/elif (pick a reply) — tools you already have.
Step 1: spot a word in a message
The in keyword checks whether a word appears inside a string. With .lower(), capitals don’t matter:
"hello" in message.lower() is True if the message contains “hello” anywhere — “Hello!”, “oh hello there”, all count.
Step 2: several possible replies
Use elif to give the bot different responses for different keywords:
Notice "hello" in text or "hi" in text — the bot replies to either greeting. That’s the or from the booleans lesson, doing real work.
Step 3: keep the conversation going
A real chatbot doesn’t stop after one message. Wrap it in a while loop that keeps chatting until you type bye:
Each message pops a box; the bot replies; the loop repeats until you type bye. You built a conversation!
Make it your own 🚀
Give the bot a personality. Start from the looping version and:
- Add new keywords and replies (about a pet, a game, the weather, your favorite food).
- Make it answer to your own name.
- Add a
score-style counter that counts how many messages you sent, and have the bot mention it when you say bye. - Bonus: use a function
reply(text)that returns the bot’s response, to keep the loop tidy.
What you learned
"word" in textchecks whether a message contains a word;.lower()makes it case-proof.orlets one branch react to several keywords.- A
whileloop keeps the conversation going until a quit word. - Real interactive programs are just loops + input + decisions working together.
Next time is the grand finale of Phase II — a text adventure game — plus a look ahead at Phase III, where you’ll work with real data and a first taste of AI. 🗺️
Comments