Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 Write a function shop ) that takes the following arguments, in this ord

ID: 3601684 • Letter: P

Question

Python 3

Write a function shop ) that takes the following arguments, in this order: 1. filename: a file that you will need to input. The file contains many lines of grocery items and their cor- responding prices. In each line, the format is: name of product,price per unit. For example, a line of the file might look like t: Pringles,1.48 Note: content like , Pringles, 1 48, is a string, so you will need to convert 1·48 to a floating-point number when calculating the total price. For example, float (, 1-48') would return the floating-point 1.48. In practice your code will pass a variable to float) as an argument (not a literal string as shown here). Also, the quotation marks in the above example do not actually exist in the file they are shown just to remind you that the file contains strings. 2. shopping.list: a string that represents a list of things that you want to buy during this shopping trip. The format is: name of product 1, how much 1 you want, name of product 2, how much 2 you wat, etc... - For example: 'Pringles, 3,Butter,1' means you are buying 3 units of Pringles and 1 unit of Butter. Hint: build a dictionary based on the file and look up prices by product names. Note: If the shopping list string is empty, return 0.0. If there is an item in the shopping list that does not exist in the file, skip the item and continue processing the rest of the shopping list. Examples: Content of 'prices1.txt' (first line is not empty and all content is text) Chicken Quarters,1.49 Chicken Whole,1.19 Beef Ground, 2.99 Apples, .99 Bananas, .39 Raspberries,1.99 Content of 'prices2.txt' (first line is not empty and all content is text) Avocados,1.25 Carrots,1.00 Corn, .25 Pears, 1.00 Broccoli, .99 Ketchup, 2.49 Mustard,1.79 Vinegar,1.88 Content of 'prices3.txt (first line is not empty and all content is text) Honey, .38 Maple Syrup, .56 Free Rice, 0.0 Tuna, 1 .49 Garlic, 2.99 Ginger, 3.99 ist1 - list2Ketchup, 4, Mustard, 10, tooth paste, 1,Avocados, 1 list3-Tuna, 1, Honey, 1,Free Rice, 79, Ginger Bread, 4' Function Call shop (' prices1.txt, list1)0.0 shop ('prices2.txt',list2) 29.11 shop ('prices3.txt Return Value ist 3)

Explanation / Answer

def shop(filename, shopping_list):

if len(shopping_list) == 0:

return 0.0

item_dict = {}

with open(filename) as fh:

for line in fh:

(item, price) = line.split(",")

item_dict[item] = float(price)

total = 0

shopping_list = shopping_list.split(",")

for i in range(0, len(shopping_list), 2):

if shopping_list[i] in item_dict:

total += int(shopping_list[i+1])*item_dict[shopping_list[i]]

return total

# copy pastable code link: https://paste.ee/p/MjfM6