You've learned that input() lets your program ask the user for a value, and that it always gives back a string. Before you can do math with what someone types, you have to convert it with int() or float().
A coffee shop wants a tiny program for the counter. The cashier types what the customer ordered, how many, and the price of one, and the program prints the order line with the total.
The three input() lines are already written for you. Right now quantity and price are still strings, so multiplying them would fail.
Your tasks
1. Convert quantity to a whole number with int()
2. Convert price to a decimal with float()
3. Store quantity * price in total
4. Print the order line in this exact format, with the total shown as money (two decimal places):
3 x Coffee = $13.50
Try it
Press Run and type the answers when the program asks. Typing Coffee, then 3, then 4.50 should finish with:
3 x Coffee = $13.50
How it is checked
The checks run your program four times, typing a different order each time:
| Item | Quantity | Price | Last line printed |
|---|
| Coffee | 3 | 4.50 | 3 x Coffee = $13.50 |
| Bagel | 2 | 2.25 | 2 x Bagel = $4.50 |
| Muffin | 1 | 3 | 1 x Muffin = $3.00 |
| Iced tea | 12 | 1.99 | 12 x Iced tea = $23.88 |
Hints
- You can convert as you read:
quantity = int(input("Quantity: ")) - To show money with two decimal places, use an f-string with
:.2f, like f"${total:.2f}". A $ in front of the braces is just text. - If you see
TypeError: can't multiply sequence by non-int, one of your numbers is still a string.
Constraints
- Use
int() for the quantity and float() for the price - Keep the three prompts so the cashier knows what to type
- Print the total with exactly two decimal places