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.
- A variable is a labelled box.
naamis the label, the typed name is inside. f"...{naam}..."is an f-string: Python swaps{naam}for what is in the box..strip().title()cleans the name: " raj " becomes "Raj".
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
- Press Run: the Input box has a name on the first line. Change the first line to your own name and run again.
- Change
bhaitojiin the replies. - Add a second box,
shehar = input("Aap kahan se ho? "), and print it in the greeting.
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