Page 40 - ipp11
P. 40
37. (a) Write a menu-driven program to perform the following list operations: (5)
(i) Add multiple elements to the list at once
(ii) Find the index of a specific element
(iii) Count occurrences of a specific element in the list
(iv) Reverse the list
(v) Remove duplicates from the list
(vi) Find the maximum and minimum values in the list
(vii) Display the final list
OR
(b) Write a menu-driven program that manages an Inventory Management System. The program should
allow users to add products, update stock, sell items and display inventory records.
The menu should display the following:
(i) Add a New Product
(ii) Update Product Stock
(iii) Sell a Product
(iv) Display Sorted Inventory Records
(v) Remove a Product from Inventory
(vi) Exit
Ans. (a) my_list = []
while True:
print("\nMenu:")
print("1. Add multiple elements to the list")
print("2. Find the index of an element")
print("3. Count occurrences of an element")
print("4. Reverse the list")
print("5. Remove duplicates from the list")
print("6. Find maximum and minimum values")
print("7. Display the final list")
print("8. Exit")
choice = int(input("Enter your choice (1-8): "))
if choice == 1:
elements = input("Enter number of elements separated by space: ").split()
my_list.extend(elements)
print("Elements added successfully!")
elif choice == 2:
element = input("Enter the element to find its index: ")
if element in my_list:
print("Index of", element, my_list.index(element))
else:
print("element not found in the list.")
elif choice == 3:
element = input("Enter the element to count occurrences: ")
print("element occurs", my_list.count(element), "times")
elif choice == 4:
my_list.reverse()
print("List reversed successfully!")
elif choice == 5:
new_list = []
for item in my_list:
if item not in new_list:
new_list.append(item)
M.10