Text processing · 14 / 14 Exercise Medium

Shopping receipt

Objective

Create a Python program that prints a receipt with well-aligned columns using f-string formatting.

Instructions

  • Read an integer n and then n lines with the format product price (the product is a single word and the price a decimal number).
  • Show one line per product: the name left-aligned in 12 positions and the price right-aligned in 8 positions with two decimals ({name:<12}{price:>8.2f}).
  • Then show a line with 20 hyphens (-).
  • Finish with a TOTAL line with the same format, with the sum of the prices.
Input
3
pan 1.2
leche 0.95
huevos 2.35
Output
pan             1.20
leche           0.95
huevos          2.35
--------------------
TOTAL           4.50

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise