187. Python Program to Print a Shopping Cart of Objects
Medium__str__
Overview
A list of objects, each printing itself with __str__.
Problem Statement
Define a class Item with a name, a price (a decimal number) and a quantity (a whole number). Give it:
- a
total()method that returns the price times the quantity, and - a
__str__method that returns a line like4 x Pen = 6.00, with the total to two decimals.
Read the number of items, then one item per line: its name, price and quantity, separated by spaces. Make an Item for each and keep them in a list. Then print every item in the list, and finally the total of the whole cart, like Total: 32.50.
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
The number of items, then one item per line. Example:
3
Pen 1.5 4
Notebook 3.25 2
Bag 20 1
Output
The output for that example:
4 x Pen = 6.00
2 x Notebook = 6.50
1 x Bag = 20.00
Total: 32.50
The tests try 3 different inputs.