Using List Collection Types
Using List Collection type. Create a program that will allow the user to perform the following functions: (add, update, search, delete, display, and sort) items in a list:
Note: You are free to decide what data you will be storing in the list and name the list based on the type of data you wish to store.
1. The user can add items in the list until the user presses x to stop
2. The user should be able to perform search if an item exists – Display if found or not found and count the number of instance in the list.
3. The user should also be given the option to remove an item in the list – Display the message “Item found and deleted” once deletion is performed – else display “Item not found – deletion unsuccessful”
4. The user may also opt to view items in the list and display items sorted in ascending order
5. The user may opt to exit the program by typing 0
Note: you are free to design the interface of the program, based on the menu options shown.
items = []
choice = -1
print("=====| Welcome |=====")
print("=====| My Listing |=====")
while choice != 0:
print("\n==========[ MENU OPTIONS ]==========")
print("1 - Add Items")
print("2 - Search for an Item")
print("3 - Remove an Item")
print("4 - View all Items (Sorted A-Z or Z-A)")
print("0 - Exit Program")
print("====================================")
try:
choice = int(input("Pick one [0 to quit]: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == 1:
while True:
user_input = input("Enter item (type 'x' to stop): ")
if user_input.lower() == "x":
break
items.append(user_input)
print("Items added successfully.")
elif choice == 2:
search_item = input("Enter item to search: ")
count = items.count(search_item)
if count > 0:
print(f"'{search_item}' found {count} time(s).")
else:
print("Item not found.")
elif choice == 3:
remove_item = input("Enter item to remove: ")
if remove_item in items:
items.remove(remove_item)
print("Item found and deleted.")
else:
print("Item not found - deletion unsuccessful.")
elif choice == 4:
if not items:
print("No items to display.")
else:
sort_choice = input("Sort A-Z or Z-A? (Enter 'A' or 'Z'): ").upper()
if sort_choice == "A":
sorted_items = sorted(items)
elif sort_choice == "Z":
sorted_items = sorted(items, reverse=True)
else:
print("Invalid sort choice. Defaulting to A-Z.")
sorted_items = sorted(items)
print("\nItems in the list:")
for i, item in enumerate(sorted_items, start=1):
print(f"{i}. {item}")
elif choice == 0:
print("Exiting program... Goodbye!")
else:
print("Invalid option. Please try again.")
=====| Welcome |=====
=====| My Listing |=====
==========[ MENU OPTIONS ]==========
1 - Add Items
2 - Search for an Item
3 - Remove an Item
4 - View all Items (Sorted A-Z or Z-A)
0 - Exit Program
====================================
Pick one [0 to quit]: 1
Enter item (type 'x' to stop): 1
Enter item (type 'x' to stop): 2
Enter item (type 'x' to stop): 3
Enter item (type 'x' to stop): 4
Enter item (type 'x' to stop): 5
Enter item (type 'x' to stop): cca
Enter item (type 'x' to stop): cca
Enter item (type 'x' to stop): lolo
Enter item (type 'x' to stop): lola
Enter item (type 'x' to stop): eksena
Enter item (type 'x' to stop): x
Items added successfully.
==========[ MENU OPTIONS ]==========
1 - Add Items
2 - Search for an Item
3 - Remove an Item
4 - View all Items (Sorted A-Z or Z-A)
0 - Exit Program
====================================
Pick one [0 to quit]: 3
Enter item to remove: cca
Item found and deleted.
==========[ MENU OPTIONS ]==========
1 - Add Items
2 - Search for an Item
3 - Remove an Item
4 - View all Items (Sorted A-Z or Z-A)
0 - Exit Program
====================================
Pick one [0 to quit]: 4
Sort A-Z or Z-A? (Enter 'A' or 'Z'): A
Items in the list:
1. 1
2. 2
3. 3
4. 4
5. 5
6. cca
7. eksena
8. lola
9. lola
==========[ MENU OPTIONS ]==========
1 - Add Items
2 - Search for an Item
3 - Remove an Item
4 - View all Items (Sorted A-Z or Z-A)
0 - Exit Program
====================================
Pick one [0 to quit]: 0
Exiting program... Goodbye!