Print an invoice with straight columns and a total.
What to build
A small table, in which every row uses the same widths.
Requirements
- Read three lines, each with an item, a quantity and a unit price.
- Print each item with its name left-aligned in 10, its quantity right-aligned in 4 and its cost right-aligned in 10 with two decimals.
- Print a last row with
Totalleft-aligned in 14 and the sum right-aligned in 10 with two decimals.
Input
Three lines: ITEM QTY PRICE.
Output
Four lines, as in the example.
Constraints
Names have at most 10 letters. Quantities are whole numbers from 0 to 999, and prices are from 0 to 999.
Example
Input:
Rice 2 68.5
Dal 1 120
Oil 3 175.25
Output:
Rice 2 137.00
Dal 1 120.00
Oil 3 525.75
Total 782.75
Why it works
The name and quantity columns take up 14 characters together, so a label of width 14 keeps the total under the cost column.
Examples
Example 1 · Input
Rice 2 68.5 Dal 1 120 Oil 3 175.25
Expected output
Rice 2 137.00 Dal 1 120.00 Oil 3 525.75 Total 782.75
Example 2 · Input
Pen 10 5 Book 1 250 Bag 0 900
Expected output
Pen 10 50.00 Book 1 250.00 Bag 0 0.00 Total 300.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Read each item with input().split().
Hint 2
Use the same format code on every row.
Hint 3
Keep a running total as you go.
Solution
Show one correct solution (try it yourself first)
name1, qty1, price1 = input().split()
name2, qty2, price2 = input().split()
name3, qty3, price3 = input().split()
cost1 = int(qty1) * float(price1)
cost2 = int(qty2) * float(price2)
cost3 = int(qty3) * float(price3)
print(f"{name1:<10}{qty1:>4}{cost1:>10.2f}")
print(f"{name2:<10}{qty2:>4}{cost2:>10.2f}")
print(f"{name3:<10}{qty3:>4}{cost3:>10.2f}")
print(f"{'Total':<14}{cost1 + cost2 + cost3:>10.2f}")