1, Program function
1. Create a multi-level student information management system that can advance and retreat
2. Mind map
2, Student entity
1. Mind map
2. Relationship model
Student (student number, name, gender, age, class, major, Department, telephone)
3. Student documents
First save the student information to a text file and separate each field with a space
3, Read student files
1. Save multiple student information with a list of tuples
Create "save multiple student information with a list of tuples. py"
''' Save multiple student information with tuple list ''' # Define 6 student tuples student1 = ('2021001', 'Li Xiaohong', 'female', '19', 'Software technology', 'College of artificial intelligence and big data', '15945456780') student2 = ('2021002', 'Xiao Gang Wang', 'male', '18', 'Software technology', 'College of artificial intelligence and big data', '13890904567') student3 = ('2021003', 'Tang Yuhan', 'female', '19', 'Software technology', 'College of artificial intelligence and big data', '18878789023') student4 = ('2021101', 'Zhang Sanfeng', 'male', '18', 'Big data technology', 'College of artificial intelligence and big data', '15945456780') student5 = ('2021102', 'Xiao rainforest', 'male', '18', 'Big data technology', 'College of artificial intelligence and big data', '18890904560') student6 = ('2021103', 'Zheng Xiaocui', 'female', '19', 'Big data technology', 'College of artificial intelligence and big data', '15890904567') # Create a list of student tuples students = [] students.append(student1) students.append(student2) students.append(student3) students.append(student4) students.append(student5) students.append(student6) # Traverse student list for student in students: for i in range(len(student)): print(student[i],end=' ') print()
Run the program and view the results
2. Read the student file data and save it to the tuple list
First look at the student file location
Create "read student file and save it to the list of elements. py"
''' Read the student file and save it to the tuple list ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuples to student list students.append(student) #Format output student list for student in students: for i in range(len(student)): print(student[i],end=' ') print()
Run the program to view the results
3. Students encounter problems in operation
If you change the file code to the one shown in the figure above, there will be no file operation problem. There will be no error when reading the file. To read the file, remember to put the created program in the same folder as the file.
4, Add, delete, modify and query the list of student tuples
1. Add student records
''' Add student tuples to list ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuples to student list students.append(student) # Add a student tuple new_student = ('2121004', 'Wu Wenyan', 'female', '18', '2021 Level 4 software class', 'Software technology', 'College of artificial intelligence and big data', '12345678901') students.append(new_student) # Output updated student tuple list for student in students: for i in range(len(student)): print(student[i], end=' ') print()
Run the program to view the results
2. Query student records
(1) Query student records by student number
''' Query student records by student number ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuples to student list students.append(student) # The user inputs the student number from the keyboard id = input('Please enter the student number to find the student:') found = False for student in students: if (student[0] == id ): found = True for i in range(len(student)): print(student[i], end=' ') break if not found: print('Warm tip: the queried student does not exist!'.format(id))
Run the program to view the results
(2) . query by name
''' Query by name ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuples to student list students.append(student) # The user inputs the student number from the keyboard name = input('Please enter the name of the student you are looking for:') found = False for student in students: if (student[1] == name ): found = True for i in range(len(student)): print(student[i],end= ' ') if not found: print('Warm tip: the queried student does not exist!'.format(name))
Run the program to view the results
3. Modify student records
''' Modify student records ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuple to student list students.append(student) # Enter the student number of the student to be changed id = input('Enter the student number of the student to be changed:') found = False for student in students: if (student[0] == id): found = True # Convert student tuples to lists student = list(student) new_age = input('Enter the modified age:') new_telephone = input('Enter a new number:') # Modify the value of the attribute specified by the student tuple student[3] = new_age student[7] = new_telephone # Restore list to metagroup student = tuple(student) print('Congratulations: student record modified successfully!') for i in range(len(student)): print(student[i], end=' ') break if not found: print('Student number is{}My students didn't find it!'.format(id))
Run the program to view the results
4. Delete student records
''' Delete student record ''' # Create an empty student list students = [] #Open file as read-only f = open('student.txt') # Traverses the file object in rows for line in f: # Remove the newline character from each line line = line.replace('\n', '') # Split each line with a space as a separator student = tuple(line.split(' ')) #Add student tuples to student list students.append(student) # Enter the student number of the student to be deleted id = input('Enter the student number of the student to be deleted:') found = False for student in students: if (student[0] == id): found = True #Delete list elements by value students.remove(student) print('Congratulations: student record deleted successfully!') break if not found: print('Student number is{}My students didn't find it!'.format(id)) else: for student in students: for i in range(len(student)): print(student[i], end=' ') print()
Run the program to view the results
5, Writes a list of student tuples to a file
Write "write the list of student tuples to the file. py"
''' Writes a list of student tuples to a file ''' # Define 6 student tuples student1 = ('2021001', 'Li Xiaohong', 'female', '90', 'Software technology', 'College of artificial intelligence and big data', '15945456780') student2 = ('2021002', 'Xiao Gang Wang', 'male', '18', 'Software technology', 'College of artificial intelligence and big data', '13890904567') student3 = ('2021003', 'Tang Yuhan', 'female', '19', 'Software technology', 'College of artificial intelligence and big data', '18878789023') student4 = ('2021101', 'Zhang Sanfeng', 'male', '18', 'Big data technology', 'College of artificial intelligence and big data', '15945456780') student5 = ('2021102', 'Xiao rainforest', 'male', '18', 'Big data technology', 'College of artificial intelligence and big data', '18890904560') student6 = ('2021103', 'Zheng Xiaocui', 'female', '19', 'Big data technology', 'College of artificial intelligence and big data', '15890904567') # Create a list of student tuples students = [] students.append(student1) students.append(student2) students.append(student3) students.append(student4) students.append(student5) students.append(student6) # Output student list print(students) # Create a writable file f = open('student.txt', 'w') for student in students: student_line = ' '.join(student) + '\n' f.write(student_line) # Close file f.close() print('The list of student tuples was written to the file successfully!')
Run the program to view the results
Then we refresh whether the student file has been written successfully.
Successfully written!
6, Constructing a multi-level menu system for student information management
Build a complete multi-level menu system
Programming - student information management py
All functions such as addition, deletion, modification and query are improved and operated completely.
def add_student(): students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) id = input('Student No.:') name = input('full name:') gender = input('Gender:') age = input('Age:') grade = input('Class:') major = input('Major:') department = input('Department:') telephone = input('Telephone:') students.append([id, name, gender, age, grade, major, department, telephone]) f = open('student.txt', 'w') for i in range(len(students)): student = ' '. join(students[i]) + '\n' f.write(student) f.close() print('\n Congratulations, student record added successfully!\n') def query_student(): while True: print('Query student records') print('=' * 12) print('1.Query student records by student number') print('2.Query student records by name') print('3.Query all student records') print('4.Previous Menu ') print('=' * 12) mc3 = int(input('Enter menu number:')) if mc3 == 1: students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) id = input('Please enter the student number to find the student:') found = False for student in students: if (student[0] == id): found = True for i in range(len(student)): print(student[i], end=' ') break if not found: print('Warm tip: the queried student does not exist!'.format(id)) elif mc3 == 2: students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) name = input('Please enter the name of the student you are looking for:') found = False for student in students: if (student[1] == name): found = True for i in range(len(student)): print(student[i], end=' ') if not found: print('Warm tip: the queried student does not exist!'.format(name)) elif mc3 == 3: students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) for student in students: for i in range(len(student)): print(student[i], end=' ') print() else: break def modify_student(): students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) id = input('Enter the student number of the student to be changed:') found = False for student in students: if (student[0] == id): found = True student = list(student) new_age = input('Enter the modified age:') new_telephone = input('Enter a new number:') student[3] = new_age student[7] = new_telephone student = tuple(student) print('Congratulations: student record modified successfully!') for i in range(len(student)): print(student[i], end=' ') break if not found: print('Student number is{}My students didn't find it!'.format(id)) f = open('student.txt', 'w') for i in range(len(students)): student = ' '.join(students[i]) + '\n' f.write(student) f.close() def delete_student(): students = [] f = open('student.txt') for line in f: line = line.replace('\n', '') student = tuple(line.split(' ')) students.append(student) id = input('Enter the student number of the student to be deleted:') found = False for student in students: if (student[0] == id): found = True students.remove(student) print('Congratulations: student record deleted successfully!') break if not found: print('Student number is{}My students didn't find it!'.format(id)) else: for student in students: for i in range(len(student)): print(student[i], end=' ') print() f = open('student.txt', 'w') for i in range(len(students)): student = ' '.join(students[i]) + '\n' f.write(student) f.close() def login(): username = input('Enter user name:') password = input('Input password:') if username == 'XT' and password == '2021': while True: print('\n Student management system') print('=' * 13) print('1.Add student records') print('2.Query student records') print('3.Modify student records') print('4.Delete student record') print('5.Previous Menu ') print('=' * 13) mc2 = int(input('Enter menu number:')) if mc2 == 1: add_student() elif mc2 == 2: query_student() elif mc2 == 3: modify_student() elif mc2 == 4: delete_student() else: break else: print('\n Wrong user name or password, please login again!\n') # main program while True: print('User login') print('=' * 8) print('1.Sign in') print('2.sign out') print('=' * 8) mc1 = int(input('Enter menu number:')) if mc1 == 1: login() elif mc1 == 2: print('\n Thank you for using this program!\n') break
7, Summary
The student management system is still a little difficult, and you should be careful when writing, otherwise it is easy to make mistakes. Write in the way you are good at, which is convenient for subsequent correction.
Whether python is difficult or not is not simple, mainly because everyone 1 should insist on doing it.