question 162 Nested Data Structures · easy
Invoice totals that stop at one
invoice_totals should return the total of every invoice (quantity × price over its lines). The finance team says the report only ever shows the first invoice. Find the bug and fix it.
Function
invoice_totals(invoices)invoicesdictionaries with
invoice and lines: a list of {"stock_code", "quantity", "price"}Returns
{invoice number: total} for every invoice, in order; an invoice with no lines totals 0Keepthe function name and what it returns; only the bug should change
Examples
callinvoice_totals([{'invoice': '489434', 'lines': [{'stock_code': '85048', 'quantity': 12, 'price': 6.95}, {'stock_code': '79323P', 'quantity': 12, 'price': 6.75}]}, {'invoice': '489435', 'lines': [{'stock_code': '22350', 'quantity': 12, 'price': 2.55}]}, {'invoice': 'C489449', 'lines': [{'stock_code': '22087', 'quantity': -12, 'price': 2.95}]}])returns{'489434': 164.4, '489435': 30.6, 'C489449': -35.4}
callinvoice_totals([{'invoice': '489435', 'lines': [{'stock_code': '22350', 'quantity': 12, 'price': 2.55}]}])returns{'489435': 30.6}
callinvoice_totals([{'invoice': '489434', 'lines': [{'stock_code': '85048', 'quantity': 12, 'price': 6.95}, {'stock_code': '79323P', 'quantity': 12, 'price': 6.75}]}, {'invoice': '489435', 'lines': [{'stock_code': '22350', 'quantity': 12, 'price': 2.55}]}])returns{'489434': 164.4, '489435': 30.6}