In: Computer Science
IN PYTHON
Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low as an integer, and returns a sorted list of items that are below an inventory level that is given by thelowinteger parameter. The function should work without the low parameter supplied - in which case, you should assume low is 5. You do not have to worry about reading and writing to a file, that code is provided, and you don’t have to change it. Example: If the inventory is {'banana':3,'apple':10}, check_inventory(inventory) will return the list['banana'] and print it in the output file. For the same inventory, check_inventory(inventory, 15) will return the list ['apple','banana'] and print it out in the output file.
I had written only the function for inventory since it is said that the file reading is given there you just need to call the function. If you have any queries write a comment. If understood upvote Thank you.
SOLUTION:
##define function with a default value of low as 5
def check_inventory(inventory,low=5):
result=[]##intialize the result list
for key in inventory:##iterate through the dictionary
if(inventory[key]<low):##if value is less than low
result.append(key)##append to list
result.sort()##sort the list
return result##result the list
##inventory dictionery
inventory={'banana':3,'apple':10}
print(check_inventory(inventory))## call the inventory function
with out low
print(check_inventory(inventory,15))##call the inventory with
low
CODE IMAGE: ## called with low and without low
OUTPUT: