preface
This article introduces the implementation of student management system with Python. This article is actually the first chapter of my long article. It is cut out separately here to facilitate everyone's viewing. The code in this article is executable code. It is recommended that readers read the code and write it by hand. The code is not complex, Readers only need to follow my code and code ideas to easily realize the student management system.
1. Demand analysis
Functions of student management system
1. Add student and grade information
2. Save student information to a file
3. Modify and delete student information
4. Query student information
5. Sort according to students' grades
6. Count the total scores of students
2. System design
System functional structure
Seven modules of student information management system
1. Input student information module
2. Find student information module
3. Delete student information module
4. Modify student information module
5. Student achievement ranking module
6. Module of counting the total number of students
7. Display all student information modules
System business process
3. Necessary for system development
1. System development environment
a. Operating system: win10
b.Python interpreter version: Python 3 ten
c. Development tool: PyCharm
d.Python built-in module: OS, re
2. Project directory structure
4. Main function design
Operation effect diagram of system main interface:
Business process of main function
Implement main function
number | function |
---|---|
0 | Exit the system |
1 | Enter student information and call the insert() function |
2 | To find student information, call the search() function |
3 | To delete student information, call the delete() function |
4 | Modify student information and call modify() function |
5 | Sort the students' grades and call the sort() function |
6 | Count the total number of students and call the total() function |
7 | Display all student information and call the show() function |
# Blogger: chen Chen # Blog address: https://chen-ac.blog.csdn.net/ # Development time: 12:25, January 2, 2022 # Welcome to AIoT related blogs~ def main(): while True: menu() choice = int(input('Please select:')) if choice in range(8): if choice == 0: flag = True while True: answer = input('Are you sure you want to exit the system?y/n:') if answer == 'y' or answer == 'Y': print('Thank you for your use!!!') break # Exit y/n loop elif answer == 'n' or answer == 'N': flag = False # Change the value of flag break # Exit y/n loop else: # Unknown character entered, please re-enter print('Input error,Please re-enter', end=' ') if flag: # flag = True for exit break # Exit loop else: # flag = False means no exit continue elif choice == 1: insert() elif choice == 2: search() elif choice == 3: delete() elif choice == 4: modify() elif choice == 5: sort() elif choice == 6: total() elif choice == 7: show() def menu(): print('==========================Student information management system==========================') print('-----------------------------Function menu-----------------------------') print('\t\t\t\t\t\t 1.Enter student information') print('\t\t\t\t\t\t 2.Find student information') print('\t\t\t\t\t\t 3.Delete student information') print('\t\t\t\t\t\t 4.Modify student information') print('\t\t\t\t\t\t 5.sort') print('\t\t\t\t\t\t 6.Count the total number of students') print('\t\t\t\t\t\t 7.Show all student information') print('\t\t\t\t\t\t 0.sign out') print('-----------------------------------------------------------------') def insert(): pass def search(): pass def delete(): pass def modify(): pass def sort(): pass def total(): pass def show(): pass if __name__ == '__main__': main()
.
5. Student information maintenance module design
5.1 student information entry function
Realize the function of inputting student information
Enter student information from the console and save them to a disk file
operation flow
Concrete implementation
The save(student) function is used to save student information to a file
The insert() function is used to enter student information
Because we need to repeatedly use the disk file to save information, we define a variable at the beginning of the code:
filename = 'student.txt'
insert() function:
def insert(): student_list = [] while True: id = input('Please enter ID(Such as 1001):') if not id: break name = input('Please enter your name:') if not name: break try: englist = int(input('Please enter your English score:')) python = int(input('Please enter Python achievement:')) java = int(input('Please enter Java achievement:')) except: print('Invalid input, not integer type, please re-enter') continue #Save the entered student information to the dictionary student={'id':id, 'name':name, 'english':englist, 'python':python, 'java':java} #Add student information to the list student_list.append(student) answer=input('Continue adding?y/n\n') if answer=='y': continue else: break #Call the save() function save(student_list) print('Student information entry completed!!!')
save() function:
def save(lst): try: stu_txt = open(filename,'a',encoding='utf-8') except: stu_txt = open(filename,'w',encoding='utf-8') for item in lst: stu_txt.write(str(item)+'\n') stu_txt.close()
Run instance:
Check out our student Txt file:
{'id': '1001', 'name': 'Chen chen', 'english': 60, 'python': 100, 'java': 60} {'id': '1002', 'name': 'Pretty girl', 'english': 70, 'python': 100, 'java': 90} {'id': '1003', 'name': 'Brother Owl', 'english': 80, 'python': 90, 'java': 90}
5.2 delete student information function
Realize the function of modifying student information
Enter the student ID from the console, find the corresponding student information in the disk file and modify it
operation flow
Concrete implementation
Write a function called delete() to delete student information in the main function.
The show() function is called to display student information. The function of this function will be completed later
The show() function will be implemented later. The delete() function is implemented here. You need to import the os:
def delete(): while True: student_id = input('Please enter the name of the student to delete ID: ') if student_id != '': if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as file: student_old = file.readlines() else: student_old = [] flag = False # Mark whether to delete if student_old: with open(filename, 'w', encoding='utf-8') as wfile: d = {} for item in student_old: d = dict(eval(item)) # Convert string to dictionary if d['id'] != student_id: wfile.write(str(d) + '\n') else: flag = True if flag: print(f'id by{student_id}Your student information has been deleted') else: print(f'Can't find ID by{student_id}Student information') else: print('No student information') break show() # Redisplay all student information after deletion answer = input('Continue deletion?y/n\n') if answer == 'y': continue else: break
student.txt document:
{'id': '1002', 'name': 'Pretty girl', 'english': 70, 'python': 100, 'java': 90} {'id': '1003', 'name': 'Brother Owl', 'english': 80, 'python': 90, 'java': 90}
5.3 function of modifying student information
Realize the function of modifying student information
Enter the student ID from the console, find the corresponding student information in the disk file and modify it
operation flow
Concrete implementation
Write a function called modify() to modify student information in the main function.
The show() function is called to display student information. The function of this function will be completed later
def modify(): show() if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: student_old = rfile.readlines() else: return student_id = input('Please enter the name of the student to be modified ID: ') if student_id != '': with open(filename, 'w', encoding='utf-8') as wfile: for item in student_old: d = dict(eval(item)) if d['id'] == student_id: print('Find the student information and you can modify his relevant information!') while True: try: d['name'] = input('Please enter your name:') d['english'] = input('Please enter your English score:') d['python'] = input('Please enter Python achievement:') d['java'] = input('Please enter Java achievement:') except: print('Your input is incorrect, please re-enter!!!') else: break wfile.write(str(d) + '\n') print('Modified successfully!!!') else: wfile.write(str(d) + '\n') answer = input('Continue to modify other student information?y/n\n') if answer == 'y': modify()
student.txt document:
{'id': '1002', 'name': 'Pretty girl', 'english': 70, 'python': 100, 'java': 90} {'id': '1003', 'name': 'Brother Owl', 'english': 80, 'python': 90, 'java': 90} {'id': '1001', 'name': 'Chen chen', 'english': '65', 'python': '100', 'java': '60'}
6. Query / statistics module design
6.1 function of finding student information
*Realize the function of querying student information
Enter the student ID or name from the console and find the corresponding student information in the disk file
operation flow
Concrete implementation
Write the function called search in the main function to find student information.
Define the function show to display query results_ student(query_student)
search() function:
def search(): student_query = [] while True: id = '' name = '' if os.path.exists(filename): mode = input('Press ID To find, enter 1, to find by name, enter 2:') if mode == '1': id = input('Please enter student ID:') elif mode == '2': name = input('Please enter student name:') else: print('Your input is incorrect, please re-enter') search() with open(filename, 'r', encoding='utf-8') as rfile: student = rfile.readlines() for item in student: d = dict(eval(item)) if id != '': if d['id'] == id: student_query.append(d) elif name != '': if d['name'] == name: student_query.append(d) # Display query results show_student(student_query) # clear list student_query.clear() answer = input('Do you want to continue the query?y/n\n') if answer == 'y': continue else: break else: print('Student information has not been saved yet') return
show_student() function:
def show_student(lst): if len(lst)==0: print('No student information found, no data displayed!!!') return # Define title display format format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}' print(format_title.format('ID', 'full name', 'English achievement', 'Python achievement', 'Java achievement', 'Total score')) # Define the display format of content format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}' for item in lst: print(format_data.format(item.get('id'), item.get('name'), item.get('english'), item.get('python'), item.get('java'), int(item.get('english')) +int(item.get('python')) +int(item.get('java')) ))
6.2 function of counting the total number of students
Realize the function of counting the total number of students
Count the number of student information saved in the student information file
operation flow
total() function:
def total(): if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: students = rfile.readlines() if students: print(f'Altogether{len(students)}Students') else: print('Student information has not been entered') else: print('Data information has not been saved yet.....')
6.3 function of displaying all student information
Realize the function of displaying all student information
Obtain and display all student information saved in the student information file
operation flow
show() function:
def show(): student_lst = [] if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: students = rfile.readlines() for item in students: student_lst.append(eval(item)) if student_lst: show_student(student_lst) else: print('Data has not been saved yet!!!!')
7. Sorting module design
Realize the function of sorting by students' grades
It mainly sorts the student information in ascending or descending order according to English score, Python score, Java score and total score
operation flow
sort() function:
def sort(): show() if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: student_list = rfile.readlines() student_new = [] for item in student_list: d = dict(eval(item)) student_new.append(d) else: return asc_or_desc = input('Please select(0.Ascending order 1.Descending order):') if asc_or_desc == '0': asc_or_desc_bool = False elif asc_or_desc == '1': asc_or_desc_bool = True else: print('Your input is incorrect, please re-enter') sort() mode = input('Please select sorting method(1.Sort by English score 2.Press Python Grade ranking 3.Press Java Score sorting 0.Sort by total score):') if mode == '1': student_new.sort(key = lambda x: int(x['english']), reverse=asc_or_desc_bool) elif mode == '2': student_new.sort(key = lambda x: int(x['python']), reverse=asc_or_desc_bool) elif mode == '3': student_new.sort(key = lambda x: int(x['java']), reverse=asc_or_desc_bool) elif mode == '0': student_new.sort(key = lambda x: int(x['english']) + int(x['python']) + int(x['java']), reverse=asc_or_desc_bool) else: print('Your input is incorrect, please re-enter!!!') sort() show_student(student_new)
8. Project packaging
Installing third-party modules
Online installation mode
Windows + R opens cmd and enters pip install PyInstaller
Download wait:
Download complete:
Perform packaging operations
Enter: pyinstaller -F file path
The file path is the stussystem we wrote Absolute path of PY
Then copy in the file name:
Wait for the executable file to be generated after entering:
The red circle is the address of the generated executable file. Our file can be found according to the address.
9. Summary and complete code
9.1 summary
It is said that the student management system is already a rotten Street project, but it really takes some time to complete the design and knock out the code for the first time. I have to admit that this is a small project very suitable for beginners. I hope readers can knock it independently and completely on the basis of understanding the code; The code in this paper has some shortcomings. It can only be said that it is a large template of the student management system. The code robustness is very poor. Readers can improve the code themselves and use try catch to deal with exception handling. In addition, readers are expected to master the operation of documents, which will be often used in future work.
9.2 complete code
# Blogger: chen Chen # Blog address: https://chen-ac.blog.csdn.net/ # Development time: 12:25, January 2, 2022 # Welcome to AIoT related blogs~ import os filename = 'student.txt' def main(): while True: menu() choice = int(input('Please select:')) if choice in range(8): if choice == 0: flag = True while True: answer = input('Are you sure you want to exit the system?y/n:') if answer == 'y' or answer == 'Y': print('Thank you for your use!!!') break # Exit y/n loop elif answer == 'n' or answer == 'N': flag = False # Change the value of flag break # Exit y/n loop else: # Unknown character entered, please re-enter print('Input error,Please re-enter', end=' ') if flag: # flag = True for exit break # Exit loop else: # flag = False means no exit continue elif choice == 1: insert() elif choice == 2: search() elif choice == 3: delete() elif choice == 4: modify() elif choice == 5: sort() elif choice == 6: total() elif choice == 7: show() def menu(): print('==========================Student information management system==========================') print('-----------------------------Function menu-----------------------------') print('\t\t\t\t\t\t 1.Enter student information') print('\t\t\t\t\t\t 2.Find student information') print('\t\t\t\t\t\t 3.Delete student information') print('\t\t\t\t\t\t 4.Modify student information') print('\t\t\t\t\t\t 5.sort') print('\t\t\t\t\t\t 6.Count the total number of students') print('\t\t\t\t\t\t 7.Show all student information') print('\t\t\t\t\t\t 0.sign out') print('-----------------------------------------------------------------') def insert(): student_list = [] while True: id = input('Please enter ID(Such as 1001):') if not id: break name = input('Please enter your name:') if not name: break try: englist = int(input('Please enter your English score:')) python = int(input('Please enter Python achievement:')) java = int(input('Please enter Java achievement:')) except: print('Invalid input, not integer type, please re-enter') continue #Save the entered student information to the dictionary student={'id':id, 'name':name, 'english':englist, 'python':python, 'java':java} #Add student information to the list student_list.append(student) answer=input('Continue adding?y/n\n') if answer=='y': continue else: break #Call the save() function save(student_list) print('Student information entry completed!!!') def save(lst): try: stu_txt = open(filename,'a',encoding='utf-8') except: stu_txt = open(filename,'w',encoding='utf-8') for item in lst: stu_txt.write(str(item)+'\n') stu_txt.close() def search(): student_query = [] while True: id = '' name = '' if os.path.exists(filename): mode = input('Press ID To find, enter 1, to find by name, enter 2:') if mode == '1': id = input('Please enter student ID:') elif mode == '2': name = input('Please enter student name:') else: print('Your input is incorrect, please re-enter') search() with open(filename, 'r', encoding='utf-8') as rfile: student = rfile.readlines() for item in student: d = dict(eval(item)) if id != '': if d['id'] == id: student_query.append(d) elif name != '': if d['name'] == name: student_query.append(d) # Display query results show_student(student_query) # clear list student_query.clear() answer = input('Do you want to continue the query?y/n\n') if answer == 'y': continue else: break else: print('Student information has not been saved yet') return def show_student(lst): if len(lst)==0: print('No student information found, no data displayed!!!') return # Define title display format format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}' print(format_title.format('ID', 'full name', 'English achievement', 'Python achievement', 'Java achievement', 'Total score')) # Define the display format of content format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}' for item in lst: print(format_data.format(item.get('id'), item.get('name'), item.get('english'), item.get('python'), item.get('java'), int(item.get('english')) +int(item.get('python')) +int(item.get('java')) )) def delete(): while True: student_id = input('Please enter the name of the student to delete ID: ') if student_id != '': if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as file: student_old = file.readlines() else: student_old = [] flag = False # Mark whether to delete if student_old: with open(filename, 'w', encoding='utf-8') as wfile: d = {} for item in student_old: d = dict(eval(item)) # Convert string to dictionary if d['id'] != student_id: wfile.write(str(d) + '\n') else: flag = True if flag: print(f'id by{student_id}Your student information has been deleted') else: print(f'Can't find ID by{student_id}Student information') else: print('No student information') break show() # Redisplay all student information after deletion answer = input('Continue deletion?y/n\n') if answer == 'y': continue else: break def modify(): show() if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: student_old = rfile.readlines() else: return student_id = input('Please enter the name of the student to be modified ID: ') if student_id != '': with open(filename, 'w', encoding='utf-8') as wfile: for item in student_old: d = dict(eval(item)) if d['id'] == student_id: print('Find the student information and you can modify his relevant information!') while True: try: d['name'] = input('Please enter your name:') d['english'] = input('Please enter your English score:') d['python'] = input('Please enter Python achievement:') d['java'] = input('Please enter Java achievement:') except: print('Your input is incorrect, please re-enter!!!') else: break wfile.write(str(d) + '\n') print('Modified successfully!!!') else: wfile.write(str(d) + '\n') answer = input('Continue to modify other student information?y/n\n') if answer == 'y': modify() def sort(): show() if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: student_list = rfile.readlines() student_new = [] for item in student_list: d = dict(eval(item)) student_new.append(d) else: return asc_or_desc = input('Please select(0.Ascending order 1.Descending order):') if asc_or_desc == '0': asc_or_desc_bool = False elif asc_or_desc == '1': asc_or_desc_bool = True else: print('Your input is incorrect, please re-enter') sort() mode = input('Please select sorting method(1.Sort by English score 2.Press Python Grade ranking 3.Press Java Score sorting 0.Sort by total score):') if mode == '1': student_new.sort(key = lambda x: int(x['english']), reverse=asc_or_desc_bool) elif mode == '2': student_new.sort(key = lambda x: int(x['python']), reverse=asc_or_desc_bool) elif mode == '3': student_new.sort(key = lambda x: int(x['java']), reverse=asc_or_desc_bool) elif mode == '0': student_new.sort(key = lambda x: int(x['english']) + int(x['python']) + int(x['java']), reverse=asc_or_desc_bool) else: print('Your input is incorrect, please re-enter!!!') sort() show_student(student_new) def total(): if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: students = rfile.readlines() if students: print(f'Altogether{len(students)}Students') else: print('Student information has not been entered') else: print('Data information has not been saved yet.....') def show(): student_lst = [] if os.path.exists(filename): with open(filename, 'r', encoding='utf-8') as rfile: students = rfile.readlines() for item in students: student_lst.append(eval(item)) if student_lst: show_student(student_lst) else: print('Data has not been saved yet!!!!') if __name__ == '__main__': main()