Home › AI + Python: Build a Chai Chatbot › Lesson 4
"MENU" or "menu"? .lower() + .strip()
AI + Python: Build a Chai Chatbot, lesson 4 of 8. simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
A customer types "MENU" with Caps Lock on, or adds a space by mistake: " menu". The Part 3 bot replies "Samjha nahi!", because its diary only has "menu".
For Python, "MENU", "menu" and " menu" are three different strings.
.lower() makes every letter small. .strip() removes extra spaces at the start and the end. Add both right after input(): clean the message first, then look it up.
msg.lower()on its own line changes nothing. Strings never change, so keep the result:msg = msg.lower().- The dictionary keys must be lowercase too:
"menu", not"Menu". .strip()only trims the ends. The space inside "masala chai" stays.
The code
replies = {"menu": "Masala, Adrak!", "price": "Rs 20"}
while True:
msg = input("Aap: ").lower().strip()
print(replies.get(msg, "Samjha nahi!"))
Output:
Aap: MENU Masala, Adrak! Aap: Price Rs 20 Aap: menu Masala, Adrak!
Try it yourself
- Press Run: the Input box already has " MENU ", "Price" and "menu". Then try " menu " and "PRICE".
- Add "timing": "Subah 7 - raat 10" and type "TIMING".
- Remove
.strip()and type " menu" with a space in front. What happens?
Remember
Clean it first, then look it up. (Pehle saaf karo, phir dhundo.)
← 50 if-elses? Use a dictionary · Understand whole sentences: the in word →
All lessons: AI + Python: Build a Chai Chatbot