Editor

Python Program for a Menu-Driven List

Intermediate · Lists · CBSE class 12 practical list

A menu-driven program shows numbered options, runs the one the user picks and comes back to the menu until the user chooses Exit. This program offers add, remove, sort, search and display operations on a list of numbers, a common CBSE practical question on lists.

Program

menu_driven_list.py
numbers = [25, 10, 40]
# Each step is (choice, value), standing in for what a user would type.
# With input(), the loop would be: while True: choice = int(input("Enter choice: "))
steps = [
(5, None), (1, 30), (2, 10), (2, 99), (3, None),
(4, 40), (4, 7), (8, None), (5, None), (6, None),
]
print("Menu: 1.Add 2.Remove 3.Sort 4.Search 5.Display 6.Exit")
for choice, value in steps:
print("Choice", choice, end=": ")
if choice == 1:
numbers.append(value)
print("added", value)
elif choice == 2:
if value in numbers:
numbers.remove(value)
print("removed", value)
else:
print(value, "is not in the list")
elif choice == 3:
numbers.sort()
print("list sorted")
elif choice == 4:
if value in numbers:
print(value, "found at index", numbers.index(value))
else:
print(value, "not found")
elif choice == 5:
print("the list is", numbers)
elif choice == 6:
print("exiting the menu")
break
else:
print("invalid choice")
Output
Menu: 1.Add  2.Remove  3.Sort  4.Search  5.Display  6.Exit
Choice 5: the list is [25, 10, 40]
Choice 1: added 30
Choice 2: removed 10
Choice 2: 99 is not in the list
Choice 3: list sorted
Choice 4: 40 found at index 2
Choice 4: 7 not found
Choice 8: invalid choice
Choice 5: the list is [25, 30, 40]
Choice 6: exiting the menu

How it works

  1. The steps list plays the part of the user: each pair is a menu choice and the number that goes with it.
  2. The if/elif chain runs a different list operation for each choice: append(), remove(), sort(), index() and printing the list.
  3. Remove and search check value in numbers first, because remove() and index() raise a ValueError for a value that is not in the list.
  4. Choice 6 ends the loop with break, and a number outside 1 to 6, like 8, gets an invalid choice message.
  5. In the input() version, while True: repeats the menu again and again, and break on choice 6 is the only way out.

Another way to write it

With a while loop, like the input() version

With a while loop, like the input() version
numbers = [5, 3, 8]
typed = [1, 2, 4, 3, 9] # the choices a user might type, one per round
while True:
choice = typed.pop(0) # or: choice = int(input("1.Show 2.Sum 3.Max 4.Reverse 9.Exit: "))
if choice == 1:
print("List:", numbers)
elif choice == 2:
print("Sum:", sum(numbers))
elif choice == 3:
print("Largest:", max(numbers))
elif choice == 4:
numbers.reverse()
print("Reversed:", numbers)
elif choice == 9:
print("Bye")
break
Output
List: [5, 3, 8]
Sum: 16
Reversed: [8, 3, 5]
Largest: 8
Bye

This is the shape a practical file needs: while True keeps showing the menu and only the exit choice breaks out. Replace typed.pop(0) with the input() call from the comment to make it interactive.

Try it yourself

Add a choice 7 that puts a number at the start of the list with numbers.insert(0, value), and add a step that uses it.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises