Problem A: Grocery Bag (10 points): In this problem you will create a series of
ID: 3708294 • Letter: P
Question
Problem A: Grocery Bag (10 points): In this problem you will create a series of functions that will allow a user to add grocery items to a bag, take items out of the bag, lookup items in the bag, and show the entire contents of the bag. For this problem, vou will use a dictionarv to store the users grocerv items. The functions you will need to write are below (explanations for each function are in the constraints): de f loo kup (d, Item) : def subitem (d,item) : def addItem (d,item): def show (d): This problem will also require you to create a main function that will allow users to specify the actions they would like to occur. To make this program look nicer we will be using the Unicode characters to represent each food item. We have provided a list of food items and Unicode values for them. To get the Unicode character to appear in the terminal, you will need to use the chr0 function with the decimal representation of the Unicode character as the input. For example running print (chr(127822) into a Python interpreter would show an apple (try it). We have provided a table with all of the items we will be using in testing your programs and the Unicode values associated with them. You MUST implement all of the food items in the table. [hint: use a dictionary] Here is a sample output of your program. Your program needs to resemble this program completely. In your dictionary, the key should holds the item name and respective value should holds the number counts of that item. If an item is added to the dictionary for the first time, the item name should be added as the key and the value should be 1. Then if the item is added again the value should be incremented by 1 each time. If an item not in the dictionary is called in the functions lookup, show, or subItem then nothing should happen! Remember you are only using the Unicode values in the lookup and show functions!Explanation / Answer
#Unicode for each grocery items to display in console
unicode = {"pizza": 127829, "carrot": 129365, "apple": 127822, "peach": 127825, "egg": 129370,
"roasted sweet potato": 127840, "pineapple": 127821, "cookie": 127850,
"bread": 127838, "lemon": 127819, "banana": 127820, "avocado": 129361}
#lookup specific grocery item
def lookup(d, item):
if d.get(item) is not None:
print(item, end=" ")
for i in range(0, d[item]):
print(chr(unicode[item]), end="")
print("")
#add item to grocery
def addItem(d, item):
if d.get(item) is None:
d[item] = 1
else:
d[item] += 1
#remove item from grocery
def subItem(d, item):
if d.get(item) is not None:
if (1 == d[item]):
del d[item]
else:
d[item] -= 1
#display all available grocery items
def show(d):
for item, cnt in d.items():
print(item, end=" ")
for i in range(0, cnt):
print(chr(unicode[item]), end="")
print("")
#main function to read user input
if __name__ == "__main__":
grocery = {}
while (1):
cmd = input("=> ")
cmd = cmd.lower()
if "show" == cmd:
show(grocery)
elif "quit" == cmd:
exit()
else:
(cmd, item) = cmd.split()
if "add" == cmd:
addItem(grocery, item)
elif "sub" == cmd:
subItem(grocery, item)
elif "lookup" == cmd:
lookup(grocery, item)
//Sample Output:
=> add apple
=> add carrot
=> add bread
=> add cookie
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.