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.

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

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