get ready

development environment :Windows 7

development tool :pycharm

development language :python3.7

Thinking and function analysis

* Create a student class Student, And define initialization __init__ method , Student information includes name , Age , Gender , Mathematics achievement ,python achievement , Student number attribute
* Define a method to initialize student information init_student, That is to add some test data by default .
* Define a method to read a file , It is used to read out the student information in the file and save the attribute of the class students in , For subsequent operations only
* Define a method to write a file , Used to set class properties students Re write the student information in to the file
* Define a method to check whether a student number exists , Used to check when operating students
*
Define how to add student information , First, judge whether the student number already exists , If it exists, it is always prompted to re-enter , Save the information to the class attribute until the student number does not exist students And call the write file method to write the data to the file
* Definition deletion , to update , How to query and display all student information
* Create the student object in the main function and call the initialization method to add the default data
* Circular printing prompt information , Receive user input and perform corresponding operations
code implementation
import datetime class Student(): ''' Tsinghua University ''' students = [] def
__init__(self,name='',age='',sex='',math=0,python=0,num =''): self.name = name
self.age = age self.sex = sex self.math = math self.python = python self.num =
num def __str__(self): msg = " Student information :name=%s, age=%s, sex=%s, math=%s, python=%s,
num=%s" % (self.name,self.age,self.sex,self.math,self.python,self.num) return
msg # Initialize the student information and save it to a file , A few pieces of default data are added def init_students(self): array = [ [' millet ',18,' female
',78,76,180815], [' Xiaohong ',19,' female ',87,68,180817], [' Xiao Ming ',18,' male ',98,89,180801],
[' floret ',19,' female ',67,68,180805], [' Kasumi ',17,' female ',76,87,180809]] with
open('write_data.txt','w', encoding='utf-8') as f: for row in array:
f.write('%s,%s,%s,%s,%s,%s'%(row[0],row[1],row[2],row[3],row[4],row[5]))
f.write('\n') self.read_students_from_file() def read_students_from_file(self):
with open('write_data.txt','r', encoding='utf-8') as f: for s in f: stu =
s.split(',')
self.students.append(Student(stu[0],stu[1],stu[2],stu[3],stu[4],stu[5])) def
write_students_to_file(self): with open('write_data.txt','w', encoding='utf-8')
as f: for stu in self.students:
f.write('%s,%s,%s,%s,%s,%s'%(stu.name,stu.age,stu.sex,stu.math,stu.python,stu.num))
f.write('\n') def check_stuno(self,num): no_exists = False for stu in
self.students: if num == stu.num: no_exists = True break return no_exists def
add_student(self): num = input(" Please input the student number to be added :") while self.check_stuno(num): num
= input(" The student number already exists , Please re-enter :") name = input(" Please enter the name of the student :") age = input(" Please enter student age :") sex =
input(" Please input student gender :") math = input(" Please input student's math score :") python = input(" Please input student python achievement :")
stu = Student(name,age,sex,math,python,num) self.students.append(stu)
self.write_students_to_file() def remove_student(self): num =
input(" Please input the student ID to be deleted :") for stu in self.students: if stu.num == num:
self.students.remove(stu) print(' student %s have been deleted '%num) self.write_students_to_file()
break else: print(' Student does not exist ') def update_student(self): num =
input(" Please input the student number to be modified :") for stu in self.students: if num == stu.num: stu.name =
input(" Please input student name :") stu.age = input(" Please enter student age :") stu.sex = input(" Please input student gender :")
stu.math = input(" Please input student's math score :") stu.python = input(" Please input student python achievement :")
self.students[self.students.index(stu)] = stu self.write_students_to_file()
print(' Modification succeeded ') break else: print(" Student does not exist ") def select_student(self): num =
input(" Please input the student number to be queried :") for stu in self.students: if num == stu.num:
print(" Student information found :",stu) break else: print(" Student does not exist ") def print_student_info(self):
for stu in self.students: print(stu) if __name__ == "__main__": day =
datetime.datetime.now() print("---------------------------------------------")
print(" Current time :",day) print("---------------------------------------------")
print(" Welcome to the student management system ") print("***************************") stu = Student()
stu.init_students()# Add default data while True: step = input(
"""==================================== 1. Add student information 2. Delete student information 3. Modify student information 4. Query student information
5. Display all student information 6. Exit the system ==================================== Please select an action :""") step =
int(step) if step == 1: stu.add_student() stu.print_student_info() elif step ==
2: stu.remove_student() stu.print_student_info() elif step == 3:
stu.update_student() stu.print_student_info() elif step == 4:
stu.select_student() elif step == 5: stu.print_student_info() elif step == 6:
print(' Exit the system ') break else: print(' Wrong instruction , Please re-enter ')
 

Technology