Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to write the script ' shopping.py ' . In the data folder you are given three txt files: List.txt , FoodLion.txt and HarrisTeeter.txt

You are to write the script '
shopping.py'.
In the data folder you are given three txt files: List.txt, FoodLion.txt and HarrisTeeter.txt. List.txt contains a list of the items and quantities of those items that the user wants to buy.
They could be listed as [item, quantity] or [quantity, item]. When reading the file, it is your job to differentiate between the two. I have added a comma to separate (delimit) the item name and quantity.
You are required to read the given data files, and not just copy the data.
Your code should work if we changed the contents of the data files.
HarrisTeeter.txt and FoodLion.txt contain lists of items and their price values. As a shopper you want to find the store that has your item for the cheapest price. If both stores carry your item, buy the quantity
needed from the shop selling the item for the smallest price. If only one shop sells your item, then buy your item from there, regardless of the price. If no shops sell your item, be sure to flag that item as 'Not
Sold'.
Note: These are old prices and have NOT been updated to reflect recent inflation. Plus, they were mostly made up in the first place
You should output the final shopping receipt as follows, containing the store an item was purchased from, the item name, the quantity and the total price (after multiplying the item price by its quantity). Then
you should output the total. Finally, you should put a list of items that were not found. You may format the output however you'd like as long as it is neat and all items are spaced evenly with columns aligned.
I have attached what I have completed (shopping_list I created to go through the file and create a dictionary with the items wanted and the quantity wanted)(I created stock to go through the food lion and harris teeter files and create a "stock " for them to use later when checking what store has what items), but I am not sure how to (in check_store) go through the dictionaries to check what store has the item from shopping_list and print the price with the item. Ideally the receipt would be formatted like theirs, but I am more concerned about checking if the stores have the items. import os
shop_list ={'items':[], 'quantity':[]}
def shopping_list(list):
with open(list) as file:
lines = file.readlines()
temp_shop_list ={"item" : [], "quant" : []}
for line in lines:
line.split(",")
if line[0].isdigit()== True:
temp_shop_list.update({'item' : line[1], 'quant' : line[0]})
if line[1] is type(str):
temp_shop_list.update({'item' : line[1], 'quant' : line[0]})
if line[0] is type(str):
temp_shop_list.update({'item' : line[1], 'quant' : line[0]})
if line[1].isdigit()== True:
temp_shop_list.update({'item' : line[1], 'quant' : line[0]})
else:
print("item not in shopping list")
shop_list.update(temp_shop_list)
return shop_list
ht_stock ={"ht_items": [],"ht_prices": []}
fl_stock ={"fl_items": [],"fl_prices": []}
def stock(harris_teeter, food_lion):
temp_ht_stock ={"items hold": [], "prices hold": []}
with open(harris_teeter) as file:
lines = file.readlines()
for line in lines:
line.split(" $")
temp_ht_stock.update({'items_hold': line[1], 'prices_hold': line[0]})
temp_fl_stock ={"items_hold": [], "prices_hold": []}
with open(food_lion) as file:
lines = file.readlines()
for line in lines:
line.split(" $")
temp_fl_stock.update({'items_hold': line[1], 'prices_hold': line[0]})
ht_stock.update(temp_ht_stock)
fl_stock.update(temp_fl_stock)
return ht_stock, fl_stock
def check_store(items_ht, prices_ht, items_fl, prices_fl, shop_list):
out_of_stock =[]
for item in shop_list["items"]:
if item in items_ht and item in items_fl:
if items_ht["prices"] items_fl[1]:
print(f'Harris Teeter {item}{shop_list["quantity"]}{items_ht[1]* shop_list["quantity"]}')
else:
print(f'Food Lion {item}{shop_list["quantity"]}') # need price
if item in items_ht:
print(f'Harris Teeter {item}{shop_list["items"[1]]}{ht_stock["ht_prices"[1]]* shop_list["quantity"]}')
if item in items_fl:
# append receipt
print(f'Food Lion {item}{shop_list["quantity"]}{prices_fl}')
else:
print("Item is out of stock")
out_of_stock.append(item)
if __name__=='__main__':
list = os.path.join("data", "List.txt")
food_lion = os.path.join("data", "FoodLion.txt")
harris_teeter = os.path.join("data", "HarrisTeeter.txt")
dict_list = shopping_list(list)
dict_ht = stock(harris_teeter)
dict_fl = shopping_list(food_lion)
print(f'Store Item Quantity
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lectures On Public Economics

Authors: Anthony B. Atkinson, Joseph E. Stiglitz

1st Edition

0691166412, 978-0691166414

Students also viewed these Databases questions