In: Computer Science
(Please use Python)
Suppose that a company sells five products with product codes p101, p107, p122, p125, and p126. The company has three warehouses, which are located in St. Louis, Chicago, and Kansas City. The retail value for each of the five products and the inventory for each of the warehouses are stored in dictionaries, as shown below.
Copy the code below into a code cell, and then execute that cell.
prices = {'p101':37.52, 'p117':56.98, 'p122':43.72, 'p125':48.33, 'p126':52.45}
inventory = { 'STL':{'p101':520, 'p117':315, 'p122':117, 'p125':258, 'p126':345}, 'CHI':{'p101':125, 'p117':864, 'p122':231, 'p125':612, 'p126':164}, 'KC' :{'p101':264, 'p117':285, 'p122':772, 'p125':467, 'p126':106} }
We will create a new dictionary named total_inventory that contains 5 key/value pairs. The keys should be product ids and the values should be the total quantity for that product across all three warehouses.
An outline of this process is provided for creating total_inventory is provided below:
1. Create an empty dictionary named total_inventory.
2. Loop over the keys of prices. Note that each key refers to a product id. I recommend using a variable name similar to prod_id for the loop variable. For each iteration of the loop:
a. Set a variable named quantity equal to 0.
b. Loop over the values of the elements stored in inventory. Note that these values are themselves dictionaries, and that they store the inventories for each site. For each such site inventory, use the current product id form the outer loop to extract the quantity of that product at the current site. Add this quantity to quantity.
c. Add prod_id / quantity as a new new/value pair in total_inventory.
Display the results by looping over the key/value pairs in total_inventory. Each time the loop executes, print out the pair in the following format: key: value
We will now calculate the total value of the company’s inventory.
Create a new code cell to perform the following steps:
1. Set total_value equal to 0.
2. Loop over the keys (product ids) stored in prices. Each time the loop executes:
a. Use the current product id to extract the associated values from the dictionaries prices and total_inventory.
b. Multiply these values found in Part a together and add the result to total_value.
Print the result with a message as follows:
The total value of the inventory is $xxxx.
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks! =========================================================================== prices = {'p101': 37.52, 'p117': 56.98, 'p122': 43.72, 'p125': 48.33, 'p126': 52.45} inventory = {'STL': {'p101': 520, 'p117': 315, 'p122': 117, 'p125': 258, 'p126': 345}, 'CHI': {'p101': 125, 'p117': 864, 'p122': 231, 'p125': 612, 'p126': 164}, 'KC': {'p101': 264, 'p117': 285, 'p122': 772, 'p125': 467, 'p126': 106}} # 1. Create an empty dictionary named total_inventory. total_inventory = {} # 2. Loop over the keys of prices. Note that each key refers to a product id. for key in prices.keys(): # a. Set a variable named quantity equal to 0. quantity = 0 # b. Loop over the values of the elements stored in inventory. for quant in inventory.values(): quantity += quant.get(key) # c. Add prod_id / quantity as a new new/value pair in total_inventory. total_inventory[key] = quantity # Each time the loop executes, print out the pair print(f'{key}:{quantity}') # Create a new code cell to perform the following steps: # 1. Set total_value equal to 0. total_value = 0 # 2. Loop over the keys (product ids) stored in prices. for key, price in prices.items(): # xtract the associated values from the dictionaries prices and total_inventory. quantity = total_inventory.get(key) total_value += price * quantity # Print the result with a message as follows: # The total value of the inventory is $xxxx. print(f'The total value of the inventory is ${total_value}')
==========================================================================