Question: Introduction In this lab you will develop a deeper understanding of dictionaries and lists of dictionaries which will help with hw4. I supplied a starting



Introduction In this lab you will develop a deeper understanding of dictionaries and lists of dictionaries which will help with hw4. I supplied a starting program for this called lab8_handout.py on Canvas. In this program I set up the following list of dictionaries called storeList which holds the inventory of three stores. store store1 = store2 = {"pear":23, "corn": 15, "carrot":11, "apple":99} {"apple":13, "pear":20, "corn":30, "carrot":100} {"apple":34, "pear":14, "carrot": 12, "corn":1} storeList = [storeo, store1, store2] Thus, storeList contains the list of inventory for three stores, each store contains exactly the same kinds of produce (apple, corn, pear, carrot). Remember, if you want to use a loop to walk through a dictionary you can use the .keys () method as follows: print("The inventory for store 1 is") for k in store1. keys(): print(k, store1[k]) However, I want you to get used to working with a list of dictionaries which is useful for hw4. So from here on out we will not access stored or store1 directly but access those store inventories through storeList Here's one example. Suppose I want to output how many apples are in each store. I can do this as follows: for i in range(3): print("Store", i, "has", storeList[i]["apple"], "apples") Now I want you to write a loop or a nested loop for each of the following tasks. Add these to the lab8_handout.py file and turn in the file when you're finished. You MUST use storeList which is a list of dictionaries for all these tasks. 1. Write a loop that calculates the total number of apples in all the stores and prints out the total number of apples. This should output 146 since there are 99 + 13 + 24 apples across the three stores. 2. Write a nested loop that determines the items in all the stores that have an inventory less than 15 and prints out which store and which piece of produce is running low. Your outer loop should loop through the three stores, and your inner loop should loop through the produce in the store. This should output: Store O has 11 carrot Store 1 has 13 apple Store 2 has 14 pear Store 2 has 12 carrot Store 2 has 1 corn 3. Write a nested loop that determines which item each store has the most of. The output should be: Store 0 has lots of apple with 99 Store 1 has lots of carrot with 100 Store 2 has lots of apple with 34 Grading Rubric Loop 1 is done correctly. (2 points) Loop 2 is done correctly. (4 points) Loop 3 is done correctly. (4 points) . Well done! You have completed Lab 8. Feel free to work on hw4 for the rest of the lab period. I urge you to read over hw4.pdf now if you haven't yet! Read it a few times and make sure you understand how that list of dictionaries is organized and used for the homework. Ask your TA for help today! # Lab 8 storeo = store1 = store2 = {"pear":23, "corn":15, "carrot":11, "apple":99} {"apple":13, "pear":20, "corn":30, "carrot":100} {"apple":34, "pear":14, "carrot": 12, "corn":1} storeList = [storeo, store1, store2] print("The inventory for store 1 is") for k in store1.keys(): print(k, store1[k]) print(). for i in range(3): print("Store", i, "has", storeList[i]["apple"], "apples") print()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
