Building a Student Management System with CRUD Operations in Python
The system's interface presents a menu of six operations:
- Add a new student record.
- Delete an exsiting student record.
- Update a student's details.
- Search for a student by name.
- Display the complete student roster.
- Terminate the program.
Users select an operation by entering its corresponding number.
System Interface Function
Create a function to display the available actions.
def show_menu():
print('\n' + '=' * 30)
print('Student Management System')
print('=' * 30)
print('1. Enroll New Student')
print('2. Remove Student Record')
print('3. Edit Student Information')
print('4. Look Up Student')
print('5. Show All Students')
print('6. Exit Program')
print('=' * 30)
Main Program Loop
Implement a continuous loop to handle user input, execute the chosen function, and manage errors.
# Global list to store student records.
student_roster = []
while True:
show_menu()
user_choice = input('Enter your choice (1-6): ')
if user_choice == '1':
enroll_student()
elif user_choice == '2':
remove_student()
elif user_choice == '3':
update_student()
elif user_choice == '4':
find_student()
elif user_choice == '5':
list_all_students()
elif user_choice == '6':
confirm = input('Confirm exit? (yes/no): ').lower()
if confirm == 'yes':
break
else:
print('Invalid input. Please select a number between 1 and 6.')
Core Function Implementations
1. Enroll a New Student
Checks for duplicate names before adding a new entry.
def enroll_student():
"""Registers a new student in the system."""
student_id = input('Enter Student ID: ')
student_name = input('Enter Full Name: ')
student_phone = input('Enter Phone Number: ')
global student_roster
for record in student_roster:
if student_name == record['name']:
print('Error: A student with this name is already registered.')
return
new_record = {'id': student_id, 'name': student_name, 'phone': student_phone}
student_roster.append(new_record)
print(f'Successfully enrolled {student_name}.')
2. Remove a Student Record
Searches for a student by name and deletes their record.
def remove_student():
"""Deletes a student's entry from the roster."""
target_name = input('Enter the name of the student to remove: ')
global student_roster
for entry in student_roster:
if target_name == entry['name']:
student_roster.remove(entry)
print(f'Record for {target_name} has been deleted.')
return
print('Error: Student not found in the records.')
3. Edit Student Information
Allows updating the phone number for an existing student.
def update_student():
"""Modifies an existing student's contact information."""
target_name = input('Enter the name of the student to update: ')
global student_roster
for entry in student_roster:
if target_name == entry['name']:
new_number = input('Enter the new phone number: ')
entry['phone'] = new_number
print(f'Contact info for {target_name} has been updated.')
return
print('Error: Student not found in the records.')
4. Search for a Student
Fetches and displays details for a specific student.
def find_student():
"""Searches for and prints a student's details."""
target_name = input('Enter the name to search for: ')
global student_roster
for entry in student_roster:
if target_name == entry['name']:
print('\n--- Student Details ---')
print(f"ID: {entry['id']}")
print(f"Name: {entry['name']}")
print(f"Phone: {entry['phone']}")
return
print('Error: Student not found in the records.')
5. Display All Students
Prints a formatted table of all student records.
def list_all_students():
"""Prints a complete list of all enrolled students."""
if not student_roster:
print('The student roster is currently empty.')
return
print('\n--- Complete Student Roster ---')
print('ID\t\tName\t\tPhone')
print('-' * 35)
for entry in student_roster:
print(f"{entry['id']}\t{entry['name']}\t{entry['phone']}")