Ring Up the Basket

Data with PythonEasy+10 XP+10

Ring Up the Basket

A corner shop's scanner only records a product code and a quantity for each line of a basket. The prices live in a separate price list.

Task: write basket_total(basket, prices).

  • basket is a dictionary of two lists, "code" and "qty", one entry per line. The same code can appear on more than one line.
  • prices is a dictionary of two lists, "code" and "price". Every code in the basket is in the price list, exactly once. The price list may also hold codes that aren't in the basket.
  • Each line costs its qty times its product's price.
  • Return the cost of the whole basket as a float rounded to 2 decimal places.

Examples

basket = {"code": ["A1", "B2", "A1"], "qty": [2, 1, 3]}
prices = {"code": ["A1", "B2", "C3"], "price": [1.2, 3.5, 0.8]}
returns 9.5
basket = {"code": ["milk"], "qty": [4]}
prices = {"code": ["bread", "milk"], "price": [1.45, 0.95]}
returns 3.8

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