Simple total
Input
Order([LineItem('apple', 2, 1.0), LineItem('banana', 3, 0.5)]).total()
Output
3.5
apple: 2*1.0 = 2.0; banana: 3*0.5 = 1.5; total = 3.5
Full lesson preview
Model an Order using composition: build LineItem objects and an Order that manages them and computes totals, taxes, and queries.
Problem statement
Task
Examples
Input
Order([LineItem('apple', 2, 1.0), LineItem('banana', 3, 0.5)]).total()
Output
3.5
apple: 2*1.0 = 2.0; banana: 3*0.5 = 1.5; total = 3.5
Input
Order([LineItem('book', 1, 10.0, taxable=False), LineItem('perfume', 2, 5.0, taxable=True)]).total_with_tax(0.1)
Output
21.0
Total before tax = 20.0. Only perfume is taxable: taxable amount = 10.0, tax = 1.0. Total with tax = 21.0
Input format
Output format
Constraints
Samples
Input
Order([LineItem('pen', 5, 2.0), LineItem('pencil', 2, 1.0)]).remove_item('pen'); Order([LineItem('pen', 5, 2.0), LineItem('pencil', 2, 1.0)]).total()
Output
12.0
Sample shows typical usage; removing in one expression isn't necessary for samples. Start value: 5*2 + 2*1 = 12.0