Home › AI + Python: Build a Chai Chatbot › Lesson 2
Teach the bot to reply (if-else)
AI + Python: Build a Chai Chatbot, lesson 2 of 8. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
In Part 1 the bot said "Chai ready!" to everything. Now it gets a brain: if-else.
if checks the first condition, elif (else if) checks the next one, and else catches everything else.
Python checks them from top to bottom and runs only the first branch that matches.
The code
while True:
msg = input("Aap: ")
if msg == "bye": break
if msg == "menu": print("Masala, Adrak!")
elif msg == "price": print("Rs 20")
else: print("Samjha nahi!")
Output:
Aap: menu Masala, Adrak! Aap: price Rs 20 Aap: kaise ho? Samjha nahi!
Try it yourself
- Add an
eliffor "hello" that replies "Namaste!". - Type "MENU" in capitals. Why does the bot not understand? (Part 4 fixes this.)
Remember
if-else is the bot's brain.
← Build a chai chatbot! · 50 if-elses? Use a dictionary →
All lessons: AI + Python: Build a Chai Chatbot