1. Add Student
2. Display All Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit
Menu-Driven Student Record Management System
AIM
To create a dictionary named student and write a menu-driven program to perform various operations: Show record, Add new student, Delete a student, Search record, Update record, Sort record, and Exit.
ALGORITHM
- Start
- Input number of students and create initial dictionary
- Initialize choice variable c = 0
- While c ≠ 7, repeat the following:
- a. Display menu options (1-7)
- b. Input user choice
- c. Execute corresponding operation:
- • Choice 1: Display all records
- • Choice 2: Add new student
- • Choice 3: Delete student by name
- • Choice 4: Search for student
- • Choice 5: Update student record
- • Choice 6: Sort and display records
- • Choice 7: Exit program
- Stop
PROGRAM
# Menu-driven Student Record Management System
n = int(input("How many students:"))
std = {}
# Initial data entry
for i in range(n):
print("Enter details of student:", i+1)
name = input("Enter name of student:")
pn = int(input("Enter Percentage:"))
std[name] = pn
c = 0
while c != 7:
print('''
1. Show record
2. Add new student
3. Delete a student
4. Search record
5. Update record
6. Sort record
7. Exit
''')
c = int(input("Enter your choice:"))
if c == 1:
for i in std:
print(i, ":", std[i])
elif c == 2:
print("Enter details of new student:")
name = input("Enter name:")
pn = int(input("Enter Percentage:"))
std[name] = pn
elif c == 3:
nm = input("Enter name to be deleted:")
f = std.pop(nm, -1)
if f != -1:
print(f, " is deleted successfully.")
else:
print(nm, " not found.")
elif c == 4:
name = input("Enter Customer Name:")
if name in std:
print(name, " found in the record.")
else:
print(name, " not found in the record.")
elif c == 5:
name = input("Enter Customer Name:")
pn = int(input("Enter percentage to modify:"))
std[name] = pn
elif c == 6:
l = sorted(std)
for i in l:
print(i, ":", std[i])
elif c == 7:
break
else:
print("Invalid Choice")OUTPUT
How many students: 2
Enter details of student: 1
Enter name of student: Alice
Enter Percentage: 85
Enter details of student: 2
Enter name of student: Bob
Enter Percentage: 78
1. Show record
2. Add new student
3. Delete a student
4. Search record
5. Update record
6. Sort record
7. Exit
Enter your choice: 1
Alice : 85
Bob : 78
Enter your choice: 2
Enter details of new student:
Enter name: Charlie
Enter Percentage: 92
Enter your choice: 6
Alice : 85
Bob : 78
Charlie : 92
CONCLUSION
Thus, the given program was successfully executed and the output was verified as per the expected result.
VIVA QUESTIONS
Why do we use a while loop instead of a for loop for the menu system?
A while loop allows the program to continue running until the user chooses to exit (option 7). A for loop would require knowing the exact number of iterations in advance, which is not suitable for menu-driven programs.
What is the purpose of using std.pop(nm, -1) in the delete operation?
The pop() method with a default value (-1) safely removes and returns the value if the key exists, or returns the default value if the key doesn't exist, preventing KeyError exceptions.
How does the sort operation work in this program?
The sorted(std) function returns a sorted list of dictionary keys (student names) in alphabetical order. We then iterate through this sorted list to display records in alphabetical order.
What happens if we try to update a student who doesn't exist?
The program will create a new entry with that name and percentage, as dictionary assignment (std[name] = pn) creates a new key-value pair if the key doesn't exist.
How can we improve the search functionality?
We could implement case-insensitive search using name.lower(), partial name matching, or search by percentage ranges. We could also display the student's percentage when found.
What are the limitations of this current implementation?
Limitations include: no data persistence after program ends, no input validation for percentages (0-100 range), no handling of duplicate names, and no option to sort by percentage.
How would you add input validation for percentage values?
We could add a while loop to check if 0 ≤ percentage ≤ 100, and use try-except blocks to handle non-numeric input, prompting the user to re-enter valid values.
How can we modify the program to save data to a file?
We could use the json module to save the dictionary to a JSON file when exiting and load it when starting the program, providing data persistence across program runs.
Related Resources
- Python Fundamentals
- Python Input/Output Tutorial
- Python Basics PDF
- Python Fundamentals MCQs
- Next Program: Number Comparison
Need Help?
Join our tuition classes for personalized guidance and doubt clearing sessions.
Register for Classes →