livepython.net

109. Python Program to Print a Neat Bill from Input Lines

HardFormatting

Overview

Put the whole chapter together: an input loop, split(), string methods to tidy the text, and f-string widths for a neat table.

Problem Statement

A shop types its bill one item per line, as item,quantity,price, and types done (in any capitals) when finished. There may be extra spaces around the parts, and the item names may be in any capitals.

Print a bill like the one below:

  • A header: Item in 14 characters on the left, Qty in 5 on the right, Amount in 11 on the right.
  • A line of 30 dashes.
  • One row per item: the item name with a capital first letter on every word (14, left), the quantity (5, right), and quantity times price with commas and two decimals (11, right).
  • Another line of 30 dashes.
  • A Total row with the total quantity and the total amount, in the same columns.

For example, for the input below, print:

Item            Qty     Amount
------------------------------
Pen               3      37.50
Notebook          2     120.00
------------------------------
Total             5     157.50

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

Any number of item lines, then done. Example:

pen,3,12.50
notebook,2,60
done

Output

The bill shown above. Spaces count, so check every width.

The tests try 4 different inputs, including amounts in the thousands and a bill with no items at all.

Learn this firstChapter 12 · Python f-string Formatting: Width, Align, Commas
solution.py
# write your code here

EXPECTED OUTPUT

1Item Qty Amount
2------------------------------
3Pen 3 37.50
4Notebook 2 120.00
5------------------------------
6Total 5 157.50

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.