Within Budget, On the Shelf

Data with PythonMedium+10 XP+10

Within Budget, On the Shelf

An online shop lets you search for products in a price band. A product only shows up if it is in stock.

Task: write in_band(products, low, high) that returns the names of the products a shopper should see.

  • products is a list of dictionaries, each with name, price and stock.
  • Keep a product when its price is from low to high, including both ends, and its stock is more than 0.
  • Return the names as a list, in the same order as products. If nothing matches, return [].

Examples

products = [{"name": "kettle", "price": 24.99, "stock": 3}, {"name": "toaster", "price": 35.0, "stock": 0}, {"name": "blender", "price": 49.5, "stock": 7}, {"name": "mixer", "price": 89.0, "stock": 2}]
low = 20
high = 50
returns ['kettle', 'blender']
products = [{"name": "mug", "price": 5.0, "stock": 10}, {"name": "plate", "price": 8.0, "stock": 4}, {"name": "bowl", "price": 6.5, "stock": 0}, {"name": "jug", "price": 12.0, "stock": 1}]
low = 5
high = 8
returns ['mug', 'plate']

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