Who Spent What?

Data with PythonMedium+10 XP+10

Who Spent What?

A shop's till stores each order with a customer number. The names are in a separate customer list. A few orders come from numbers that aren't in the list yet, and the money from those orders still counts.

Task: write spend_by_customer(orders, customers).

  • orders is a dictionary of two lists, "customer_id" and "amount", with one entry per order. There is at least one order.
  • customers is a dictionary of two lists, "customer_id" and "name". Each customer number appears once, and every name is different.
  • Return a dictionary from name to that customer's total spend, as a float rounded to 2 decimal places.
  • Every order from a number that isn't in customers goes under the name "Unknown". If there are no such orders, there is no "Unknown" key.
  • Customers with no orders are not included.

Examples

orders = {'customer_id': [1, 2, 1, 4], 'amount': [12.0, 8.5, 15.0, 6.0]}
customers = {'customer_id': [1, 2, 3], 'name': ['Ana', 'Ben', 'Cara']}
returns {'Ana': 27.0, 'Ben': 8.5, 'Unknown': 6.0}
orders = {'customer_id': [7, 9, 7], 'amount': [3.2, 10.0, 4.8]}
customers = {'customer_id': [7, 9], 'name': ['Dev', 'Eli']}
returns {'Dev': 8.0, 'Eli': 10.0}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run