In: Computer Science
Grocery List Program
please make input functions. For example, you need to confirm with the user when removing or adding, both are yes/no (y/n) responses, these can call the SAME function which will take care of making sure the input is valid.
Programming language: Python
Requirement: Please tightly follow up the rules and provide multiple functions. The output needs to be exactly the same and use try except valueerror to control input.
class Item:
see_list=["apple","banana","carrot","beetroot"]
def __init__(self,list_of_items):
self.list_of_items=list_of_items
def Menu(self):
print("--------------------------------------")
print("Available Options for Main Menu")
print("1. See List")
print("2. Display Items List")
print("3. Add items to the list")
print("4. Remove items from the list")
print("5. Search for item in the list")
print("0 Exit Main Menu")
print("--------------------------------------")
print("Enter option to continue")
option=int(input())
return option
def See_List(self):
print("List of items Available")
for item in self.see_list:
print(item)
def Display_items(self):
print("The following are the items in the list")
for num,item in enumerate(self.list_of_items): #Here enumerate is used to get track of item number. num+1 will give you the serial number of item
print(num+1,item)
def Add_items(self):
print("Enter item to add")
item_to_add=input()
if item_to_add not in self.list_of_items:
print("Please confirm yes/no to add an item into the list")
confirm=input()
if confirm=="yes":
print("Item : ",item_to_add," added to list")
self.list_of_items.append(item_to_add)
else:
print("Item not added")
pass
else:
print("Item already exists in the list")
def Remove_items(self):
print("Enter item to remove")
item_to_remove=input()
if item_to_remove not in self.list_of_items:
print("Item not present in list")
else:
print("Please confirm yes or no to remove item")
confirm=input()
if confirm=="yes":
print("Item : ",item_to_remove," removed from list")
self.list_of_items.remove(item_to_remove)
else:
print("Item not removed")
pass
def Search_items(self):
print("Enter item to search")
item_to_search=input()
if item_to_search not in self.list_of_items:
print("Item not found in list")
else:
print("Item found :",item_to_search)
if __name__=="__main__":
obj=Item([]) #Providing empty list to initialise for constructor.
ans=True
try:
while(ans):
option=obj.Menu()
if option==1:
obj.See_List()
elif option==2:
obj.Display_items()
elif option==3:
obj.Add_items()
elif option==4:
obj.Remove_items()
elif option==5:
obj.Search_items()
else:
ans=False
except:
print("An error occured!!! Program Stopped")
#Please do mind the indendation else you will get errors. I am attaching the output of the program for your reference.
#Please provide positive rating if you like my work.(This is just a comment. You can delete it)