25.4k views
3 votes
Write a program that accepts commands to carry out some operations to manage a list of names. Run the program will show the user a menu. In the following screen shot, the texts displayed by the program are in black. The commands entered by you are in blue. Your program should be able to perform the same behaviour as follows. The following screenshots go from left to right. After the commands of "A Oliver", "A Jimmy", and "A Tom", three names are added to the Accoulist. After the O command, the Accoxblat is sorted. Future U command will produce ordered Accaxlist. unless we add a new name making the current Acroullst out of order again. "D 1 " deletes the 14" item in the Axcaxdist.

User Suic
by
7.8k points

1 Answer

5 votes

Final answer:

The student's question focuses on creating a Python program to manage a list of names by adding, sorting, and deleting entries using a text-based menu. The program uses a loop and if-else statements for interaction and operations on the list.

Step-by-step explanation:

Python Program for Managing a List of Names

The student's question involves creating a program to manage a list of names with various operations such as adding names, sorting the list, and deleting entries from the list. In Python, this can be done using a menu-driven approach providing users with options to perform actions on the list. Here is an example of how such a program can be implemented:

names = []

while True:
print("1. Add name (A)")
print("2. Sort names (O)")
print("3. Delete name (D)")
print("4. Quit (Q)")

command = input("Enter command: ").upper()

if command == 'A':
name = input("Enter name to add: ")
names.append(name)
elif command == 'O':
names.sort()
elif command == 'D':
index = int(input("Enter index to delete: ")) - 1
if 0 <= index < len(names):
del names[index]
else:
print("Index out of range.")
elif command == 'Q':
break
else:
print("Invalid command.")

print("Current list of names:", names)

This script will continuously run, providing a simple text-based menu for users to interact with until they choose to quit by entering 'Q'. Users can add names to the list, which are stored in the names list. Sorting the list is done with the sort() method, and deleting an item requires the user to provide an index, which Python uses zero-based indexing, hence the subtraction of 1.

User Prakash Dahal
by
7.4k points