1, Program function
1. Overview
Using Python to complete a program for adding, deleting, modifying and checking student information mainly involves knowledge points: program control structure (realizing multi-level menus that can be advanced and retreated), file reading and writing operation (student information should be saved in files on disk), and function modularization by using functions.
2. Mind map
2, Student entity
1. Mind map
2. Relational model
- Student (student number, name, gender, age, class, major, Department, telephone)
3. Student list
- Line record Yuanzu
- Column - field - attribute
Student number | full name | Gender | Age | class | major | Department | Telephone |
---|---|---|---|---|---|---|---|
2021001 | Xiao Xiaohan | female | 19 | 2021 software technology class 4 | Software technology | College of artificial intelligence and big data | 12894763846 |
2021002 | Li Xiaohong | female | 18 | 2021 software technology class 4 | Software technology | College of artificial intelligence and big data | 18273649304 |
2021003 | Tang Lilin | female | 19 | 2021 software technology class 4 | Software technology | College of artificial intelligence and big data | 13762738377 |
2021004 | Xiao Haoyu | male | 20 | 2021 big data class 1 | Software technology | College of artificial intelligence and big data | 18987876361 |
2021005 | Xiao Gang Wang | male | 18 | 2021 big data class 1 | Software technology | College of artificial intelligence and big data | 13678363763 |
2021006 | Zhang Sanfeng | male | 20 | 2021 big data class 1 | Software technology | College of artificial intelligence and big data | 15887367678 |
-
First save the student information to a text file and separate each field with a space.
-
If you use the notepad that comes with your computer, you need to change the code to ANSI. The specific steps are as follows:
-
1) Save the saved text file as
-
2) Change encoding to ANSI
3, Read student files
- The file structure is composed of several lines and columns, which are read line by line. The string read from each line is divided into a tuple through spaces. After all lines are read, it will naturally form a list of tuples.
1. Save multiple student information with Yuanzu's list
- establish
- Operation results
2. Read the student file data and save it to the list of tuples
-
establish
-
Operation results
-
Format output students list
-
Operation results
4, Add, delete, modify and query the list of student tuples
1. Add student records
- Add a new student tuple
Student number | full name | Gender | Age | class | major | Department | Telephone |
---|---|---|---|---|---|---|---|
2021007 | Xiao Yang | male | 20 | 2021 big data class 1 | Big data technology | College of artificial intelligence and big data | 18273645767 |
- Operation results
2. Query student records
1) Query student records by student number
- Operation results
2) Find student records by name
- Operation results
- Find by other attributes, and so on
3. Modify student records
- Operation results
4. Delete student record
- Operation results
5, Writes a list of student tuples to a file
- Operation results
- View written files
6, Constructing a multi-level menu system for student information management
1. Build a multi-level menu system
- Operation results
2. Realize the student information management function module
1) Write and query all student records
to write:
- get_all_students() function
- display_all_students() function
- Operation results
2) Write and add student record function
to write
- add_student() function
- Operation results
- Check the new record in Notepad
3) Function of compiling and modifying student records
to write
- modify_student() function
- Modify the student's age and phone number
- Operation results
- If the student number does not exist
4) Write and delete student records
to write
- delete_student() function
- Operation results
- Check the new record in Notepad
- Delete succeeded
5) Write the function of querying student records by student number
to write
- display_id_student() function
- Run code
6) Write the function of querying student records by name
to write
- display_name_student() function
- Operation results
7, Student information management procedure
- code
# -*- coding: utf-8 -*- """ Function: student information management Author: Xiao Rong Date: December 28, 2021 """ def get_all_student(): students =[] f = open('student.txt') for line in f: line = line.replace('\n', ' ') student = line.split(' ') students.append(student) f.close() return students def add_student(): students = get_all_student() id = input('Student No.:') name = input('full name:') gender = input('Gender:') age = input('Age:') clas = input('Class:') major = input('Major:') department = input('Department:') telephone = input('Telephone:') students.append([id, name, gender, age, clas, 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 display_all_student(): students = get_all_student() for i in range(len(students)): for j in range(len(students)): print(students[i][j], end= ' ') print() def display_id_student(): students = get_all_student() f = open('student.txt') id = input('Please enter the student number to query:') 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('Student number is[{}]My students didn't find it!'.format(id)) def display_name_student(): students = get_all_student() f = open('student.txt') name = input('Please enter the name of the student to be checked:') found = False for student in students: if (student[1] == name): found = True for i in range(len(student)): print(student[i], end=' ') break if not found: print('Name is[{}]My students didn't find it!'.format(name)) def query_student(): while True: print('Query student records') print('=' * 20) print('1.Query student records by student number') print('2.Query student records by name') print('3.Query all student records') print('4.Return to the previous menu') print('=' * 20) mc3 = int(input('Enter menu number:')) if mc3 == 1: display_id_student() elif mc3 == 2: display_name_student() elif mc3 == 3: display_all_student() else: break def modify_student(): students = get_all_student() f = open('student.txt','w') 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 a new age:') new_telephone = input('Enter a new phone number:') student[3] = new_age student[7] = new_telephone student = tuple(student) print('Congratulations: student record modified successfully!') break if not found: print('Student number is[{}]My students didn't find it!'.format(id)) for i in range(len(students)): student = ' '.join(students[i]) + '\n' f.write(student) f.close() def delete_student(): students = get_all_student() f = open('student.txt', 'w') id = input('Enter the student number to be deleted:') found = False for student in students: if (student[0] == id): found = True students.remove(student) print('Delete record succeeded!') break if not found: print('Student number is[{}]My students didn't find it!'.format(id)) 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 == 'xiaoxiaorong' and password == '202542': while True: print('Student information management') print('=' * 20) 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('=' * 20) 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 The user name or password is incorrect. Please log in again\n') while True: print('User login') print('=' * 20) print('1.Sign in') print('2.sign out') print('=' * 20) mc1 = int(input('Enter menu number:')) if mc1 == 1: login() elif mc1 == 2: print('\n Thank you for using this program!\n') break