Everything With Tea in It

Data with PythonMedium+10 XP+10

Everything With Tea in It

A café's till lets staff type the item name, so the same drink shows up as "Green Tea", " mint TEA" and "tea". The owner wants the takings for every item that has a certain word in its name.

Task: write keyword_takings(sales, keyword).

  • sales is a list of dictionaries, each with item (text) and price (a number).
  • keyword is always given in lowercase, like "tea".
  • An item counts if its name contains keyword anywhere, ignoring capitals and spaces at the ends. Part of a longer word counts too, so "Teacake" contains "tea".
  • Return the total price of the items that count, rounded to 2 decimal places. If none count, return 0.0.

There is always at least one sale.

Examples

sales = [{"item": "Green Tea", "price": 2.4}, {"item": " mint TEA", "price": 2.2}, {"item": "Latte", "price": 3.1}, {"item": "Teacake", "price": 1.8}]
keyword = "tea"
returns 6.4
sales = [{"item": "Latte", "price": 3.1}, {"item": "Flat White", "price": 3.0}]
keyword = "tea"
returns 0.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