Home › AI + Python: Build a Chai Chatbot › Lesson 7

Remember the name: input + variable

AI + Python: Build a Chai Chatbot, lesson 7 of 8. simple English notes and code you can run, free.

Open the interactive lesson with Run buttonWatch on YouTube

The idea

A good chai bhaiya remembers your name. Your bot does not even ask! Today it asks once, and remembers.

input() listens: whatever you type goes into your program. Put it in a box called a variable: naam = input("Aapka naam? ").

Now naam holds the name. Put an f before a string and write {naam} inside it: f"Namaste {naam}!" fills in the name.

The code

import random
naam = input("Aapka naam? ").strip().title()
print(f"Namaste {naam}! Aaj kaun si chai?")
replies = {"menu": ["Masala, Adrak!", "Aaj special: Elaichi chai!", "Kadak masala try karo!"],
           "price": ["Rs 20", "Sirf 20 rupaye!", "Bees rupaye, bhaiya!"]}
while True:
    msg = input(f"{naam}: ").lower().strip()
    if msg == "bye":
        print(f"Phir aana, {naam} bhai!")
        break
    for word in replies:
        if word in msg:
            print(f"{naam} bhai, {random.choice(replies[word])}")

Output:

Aapka naam? Raj
Namaste Raj! Aaj kaun si chai?
Raj: menu batao
Raj bhai, Masala, Adrak!
Raj: price kya hai
Raj bhai, Sirf 20 rupaye!
Raj: bye
Phir aana, Raj bhai!

Try it yourself

Remember

input() listens, a variable remembers, an f-string speaks the name. Next: Part 8, adding up an order.

← A new reply every time: random.choice · Bill jodo: int + maths →

All lessons: AI + Python: Build a Chai Chatbot