Skip to content

22.Write a menu-driven Python program to store product names in a tuple and perform the following to : Display all products. Search a product. Count occurrences of a product. Display product position. Display products alphabetically.

ASSIGNMENT

Q.22

Q 22.Write a menu-driven Python program to store product names in a tuple and perform the following to : Display all products. Search a product. Count occurrences of a product. Display product position. Display products alphabetically.

# Menu-driven program to perform operations on product tuple products = [] n = int(input("Enter the number of products: ")) for i in range(n): product = input("Enter product name " + str(i + 1) + ": ") products.append(product) products = tuple(products) while True: print("\n========== PRODUCT MENU ==========") print("1. Display All Products") print("2. Search a Product") print("3. Count Occurrences of a Product") print("4. Display Product Position") print("5. Display Products Alphabetically") print("6. Exit") print("==================================") choice = int(input("Enter your choice: ")) if choice == 1: # Display all products print("Products:", products) elif choice == 2: # Search a product product = input("Enter product name to search: ") if product in products: print("Product found.") else: print("Product not found.") elif choice == 3: # Count occurrences of a product product = input("Enter product name: ") count = products.count(product) print("Number of occurrences:", count) elif choice == 4: # Display product position product = input("Enter product name: ") if product in products: print("Product position:", products.index(product)) else: print("Product not found.") elif choice == 5: # Display products alphabetically alphabetical = tuple(sorted(products)) print("Products in alphabetical order:", alphabetical) elif choice == 6: # Exit print("Program terminated.") break else: print("Invalid choice. Please try again.")