Home › AI + Python: Build a Chai Chatbot › Lesson 8
Bill jodo: int + maths
AI + Python: Build a Chai Chatbot, lesson 8 of 8. simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
3 chai and 2 samosa: how much is the bill? Today the bot adds it up for you.
First write the prices in variables: chai_price = 10 and samosa_price = 15.
There is one catch. input() always gives you text, even if you type 3. You cannot multiply the text "3".
Wrap it in int(): int(input("Kitni chai? ")). Now it is a real number, and Python can do the maths.
chai * chai_price + samosa * samosa_price: multiply first, then add.- 3 chai × 10 = 30, and 2 samosa × 15 = 30. The bill is 60 rupees.
int()turns the text "3" into the number 3.int("abc")cannot, and the program stops with an error (Part 9 fixes that).
The code
chai_price = 10
samosa_price = 15
naam = input("Aapka naam? ").strip().title()
chai = int(input("Kitni chai? "))
samosa = int(input("Kitne samosa? "))
bill = chai * chai_price + samosa * samosa_price
print(f"{naam} bhai, aapka bill {bill} rupaye!")
Output:
Aapka naam? Raj Kitni chai? 3 Kitne samosa? 2 Raj bhai, aapka bill 60 rupaye!
Try it yourself
- Press Run: the Input box has your name, 3 chai and 2 samosa. Change the numbers and run again.
- Add a third item:
vada_price = 12and ask "Kitne vada?". Add it to the bill. - Type
abcinstead of a number and press Run. What error do you see? (Part 9 fixes it.)
Remember
input() gives text, int() turns it into a number. Next: Part 9, what if someone types abc?
← Remember the name: input + variable
All lessons: AI + Python: Build a Chai Chatbot