Introduction:
This is a menu-driven program written in Python that allows the user to perform various operations on a list. The user can choose options like appending an element, inserting an element at a desired position, deleting an element by its position or value, modifying an element, sorting the list, and displaying the list. The program provides a menu with numbered options for the user to select from.
Code:
#Menu driven program to do various list operations
myList = [22,4,16,38,13] #myList already has 5 elements
choice = 0
while True:
print("The list 'myList' has the following elements", myList)
print("\nL I S T O P E R A T I O N S")
print(" 1. Append an element")
print(" 2. Insert an element at the desired position")
print(" 3. Append a list to the given list")
print(" 4. Modify an existing element")
print(" 5. Delete an existing element by its position")
print(" 6. Delete an existing element by its value")
print(" 7. Sort the list in ascending order")
print(" 8. Sort the list in descending order")
print(" 9. Display the list")
print(" 10. Exit")
choice = int(input("ENTER YOUR CHOICE (1-10): "))
#append element
if choice == 1:
element = int(input("Enter the element to be appended: "))
myList.append(element)
print("The element has been appended\n")
#insert an element at desired position
elif choice == 2:
element = int(input("Enter the element to be inserted: "))
pos = int(input("Enter the position:"))
myList.insert(pos,element)
print("The element has been inserted\n")
#append a list to the given list
elif choice == 3:
newList = eval(input( "Enter the elements separated by commas"))
myList.extend(list(newList))
print("The list has been appended\n")
#modify an existing element
elif choice == 4:
i = int(input("Enter the position of the element to be modified: "))
if i < len(myList):
newElement = int(input("Enter the new element: "))
oldElement = myList[i]
myList[i] = newElement
print("The element",oldElement,"has been modified\n")
else:
print("Position of the element is more than the length of list")
#delete an existing element by position
elif choice == 5:
i = int(input("Enter the position of the element to be deleted: "))
if i < len(myList):
element = myList.pop(i)
print("The element",element,"has been deleted\n")
else:
print("\nPosition of the element is more than the length of list")
#delete an existing element by value
elif choice == 6:
element = int(input("\nEnter the element to be deleted: "))
if element in myList:
myList.remove(element)
print("\nThe element",element,"has been deleted\n")
else:
print("\nElement",element,"is not present in the list")
#list in sorted order
elif choice == 7:
myList.sort()
print("\nThe list has been sorted")
#list in reverse sorted order
elif choice == 8:
myList.sort(reverse = True)
print("\nThe list has been sorted in reverse order")
#display the list
elif choice == 9:
print("\nThe list is:", myList)
#exit from the menu
elif choice == 10:
break
else:
print("Choice is not valid")
print("\n\nPress any key to continue..............")
ch = input()
Logic:
- The program uses a while loop to continuously display the menu and accept the user’s choice.
- Based on the choice, the program executes the corresponding operation on the list.
- The program includes error handling for invalid choices and checks for the position of elements before modifying or deleting them.
Output:
The program provides feedback for each operation. For example, when appending an element, the program prompts the user to enter the element and informs them that it has been appended. Similarly, for operations like inserting, deleting, modifying, sorting, or displaying the list, the program provides appropriate feedback. If the user enters an invalid choice, the program displays a message stating that it is not valid.