AW Dev Rethought

✂️ Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away - Antoine de Saint-Exupéry

🐍 Python From Zero — 04/13: Input and Strings


Description:

In the last lesson, the quantity was written into the bill calculator. This time the customer supplies it. At our café, Asha orders two cups at ₹60 each. How does pressing the 2 key become a ₹120 receipt?


Ask a question and wait for Enter

name = input("Name: ").strip()
raw = input("Cups: ")

The text inside input() is the prompt that appears on screen. Type Asha without quotes, press Enter, then type 2 at the second prompt. Python stores the answers in name and raw. The .strip() on the first line removes whitespace from the ends of the name. It keeps the space inside a name like Asha Rao.


See what changes when you convert

Python expression Result What happened
raw + raw "22" With raw holding "2", adding strings joins their characters.
int(raw) + 1 3 Converting first lets Python add numbers.

For the receipt, convert the answer once and give the number its own name:

cups = int(raw)
price = 60
total = cups * price

Now raw still holds the string "2", cups holds the integer 2, and total is 120. This example expects a positive whole number in digits. Typing two or 2.5 raises ValueError at the conversion. Python’s input() and int() reference


Put the values into a message

print(f"Order for {name}")
print(f"{cups} x ₹{price} = ₹{total}")

The f before the opening quote lets you insert values between braces. The words, currency symbol, and spaces print as written. For Asha, the program prints:

Order for Asha
2 x ₹60 = ₹120

Try removing just the f from the first line. The result becomes Order for {name}: a quick way to recognise a missing f-string prefix. Python’s formatted string literals


Your turn

Run the script again with Ravi and 3. Predict the total before pressing Enter. Then add a drink prompt so the first line can say Order for Ravi: Coffee.


Link copied!

Comments

Add Your Comment

Comment Added!