Drill

ProblemsPython › billing

Sum the invoice line items

easybillingPython

An invoice lists items with quantity and unit price. Compute the total across all lines.

invoice_line_total(items: list<LineItem>) → int

Solve it in the editor →

Where you start

def invoice_line_total(items: list[LineItem]) -> int:
    

Worked examples

CallResult
invoice_line_total([LineItem(name="Widget", qty=3, unit_price=500)])1500
invoice_line_total([LineItem(name="A", qty=2, unit_price=1000), LineItem(name="B", qty=1, unit_price=300)])2300
invoice_line_total([])0
invoice_line_total([LineItem(name="X", qty=0, unit_price=999)])0

Hint

Walk the list, multiply each line, and add.

Reference solution in Python
def invoice_line_total(items: list[LineItem]) -> int:
    return sum(item.qty * item.unit_price for item in items)

The same problem in another language

More billing problems in Python