243,715 views
16 votes
16 votes
write a function called check inventory(inventory, low) that has as arguments a dictionary inventory containing the inventory of a fruit store, and low, an integer. the function returns a list of items that are below an inventory level that is given by the low integer parameter. example: if the inventory is {'mango:22','banana':3,'apple':10}, check inventory(inventory, 5) will return the list ['banana']. for the same inventory, check inventory(inventory, 15) will return the list ['apple','banana'].

User Cherrelle
by
2.9k points

1 Answer

26 votes
26 votes

Answer:

Here is an example of a Python function that meets the requirements you specified:

def check_inventory(inventory, low):

# Initialize an empty list to store the items with low inventory

low_items = []

# Loop through the items in the inventory dictionary

for item, quantity in inventory.items():

# If the quantity is less than the low threshold, add the item to the list

if quantity < low:

low_items.append(item)

# Return the list of items with low inventory

return low_items

# Test the check_inventory function with different values of low

inventory = {'mango': 22, 'banana': 3, 'apple': 10}

print(check_inventory(inventory, 5)) # ['banana']

print(check_inventory(inventory, 15)) # ['apple', 'banana']

The check_inventory() function accepts two arguments: a dictionary inventory containing the inventory of a fruit store, and low, an integer representing the inventory level below which items are considered to be low. The function loops through the items in the inventory dictionary and checks the quantity of each item. If the quantity is less than the low threshold, the item is added to a list of low inventory items. Finally, the function returns this list of low inventory items.

In the code above, we test the check_inventory() function with different values of low to show that it works as expected. You can modify this function to suit your specific needs. For example, if you want to sort the list of low inventory items alphabetically before returning it, you can use the sort() method on the list.

User Alanning
by
2.9k points