Table of Contents
Menu Design
10 min
Data Structure
15 min
Add Student
15 min
Display Students
10 min
Search Student
15 min
Update & Delete
20 min
Menu Options

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

  1. Start
  2. Input number of students and create initial dictionary
  3. Initialize choice variable c = 0
  4. While c ≠ 7, repeat the following:
  5. a. Display menu options (1-7)
  6. b. Input user choice
  7. c. Execute corresponding operation:
  8. • Choice 1: Display all records
  9. • Choice 2: Add new student
  10. • Choice 3: Delete student by name
  11. • Choice 4: Search for student
  12. • Choice 5: Update student record
  13. • Choice 6: Sort and display records
  14. • Choice 7: Exit program
  15. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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

Need Help?

Join our tuition classes for personalized guidance and doubt clearing sessions.

Register for Classes →

Practice More

Try more Python programs to strengthen your programming skills.

View All Programs →