Python implementation of student management system (object-oriented version)

Time:2022-6-18

The example of this article shares the specific code for Python to implement the student management system for your reference. The specific contents are as follows

Realize the transition from process oriented to object-oriented, and realize object-oriented by changing the previous student management system. It also proves that process oriented can be perfectly transitioned to object-oriented, so as to further optimize the future program progress and facilitate the future program change.

The complete code is as follows:

student_ Code in main module

import student_tools


class Student(student_tools.StudentT):

    def __init__(self):
        self.user=['wangtaotao']
        self.pwd=['123456']
        student_tools.StudentT.__init__(self)

    #Login
    def denglu(self):
        Users = input ("please enter your user name:")
        PWDs = input ("please enter your password:")
        if users in self.user and pwds in self.pwd:
            self.student()
        else:
            Print ("the account or password is incorrect, please re-enter it")

    #Registration
    def zhuce(self):
        Users=input ("please enter the user name you want to register:")
        Pwds=input ("please enter the password you want to register:")
        self.user.append(users)
        self.pwd.append(pwds)
        print()
        Print ("registration succeeded!")
        print()

    #Login界面
    def dljiemian(self):

        while True:
            print("---------------------------")
            Print ("student management system login interface v1.0")
            print("                           ")
            Print ("1: login")
            Print ("2: registration")
            Print ("3: exit")
            print("                           ")
            print("---------------------------")
            Xx=input ("please enter your choice:")
            #1. login
            if xx=='1':
                self.denglu()
            elif xx=='2':
            #2. registration
                self.zhuce()
            elif xx=='3':
            #3. exit
                print()
                Print ("exited successfully!")
                print()
                break
            else:
                Print ("input error, please input again")


    #Student management system
    def student(self):
        #Call student_ Read file function in tools module
        super().read_file()
        while True:
            #Call student_ Interface functions in tools module
            super().jiemian()

            X=input ("please enter your choice:")
            #Add student
            if x=='1':
                super().add()
            #Delete student
            elif x=='2':
                super().dele()
            #Modify student
            elif x=='3':
                super().xiugai()
            #Query students
            elif x=='4':
                super().find()
            #Show all students
            elif x=='5':
                super().showall()
            #Save data to file
            elif x=='6':
                super().save_file()
            #Exit the student management system and return to the previous login interface system
            elif x=='7':
                print()
                Print ("successfully quit the student management system!")
                break
            else:
                print()
                Print ("input error, please input again")
                print()

    #Call the login interface function executed first
if __name__=='__main__':
    wtt=Student()
    wtt.dljiemian()

student_ Code in tools module

import os


class StudentT(object):

    def __init__(self):
        self.student_list=[]
        self.student_dict={}


    #Student management system interface
    @staticmethod
    def jiemian():
        print("---------------------------")
        Print ("student management system V1.0")
        print("                           ")
        Print ("1: add student")
        Print ("2: delete student")
        Print ("3: modify student")
        Print ("4: query students")
        Print ("5: show all students")
        Print ("6: save data")
        Print ("7: exit the system")
        print("                           ")
        print("---------------------------")


    #Add student
    def add(self):
        Name=input ("please enter the student name:")
        Cls=input ("please enter student class:")
        Age=input ("please enter the student age:")
        Phone=input ("please input the student's mobile number:")
        Addr=input ("please enter the student's home address:")

        self.student_dict={"name":name,"class":cls,"age":age,"phone":phone,"address":addr}

        self.student_list.append(self.student_dict)
        print()
        Print ("----- add student information interface -----")
        print()
        Print ("name \t\t", "class \t\t", "age \t\t", "telephone number \t\t", "home address \t\t")
        for student_dict_1 in self.student_list:
            print("%s\t\t%s\t\t%s\t\t%s\t\t%s" %(student_dict_1["name"],
                                                 student_dict_1["class"],
                                                 student_dict_1["age"],
                                                 student_dict_1["phone"],
                                                 student_dict_1["address"]))
        print()
        Print ("input succeeded!")
        print()

    #Delete student
    def dele(self):
        name_ Del=input ("please enter the name of the student you want to delete:")
        for student_dict_1 in self.student_list:
            if name_del in student_dict_1["name"]:
                self.student_list.remove(student_dict_1)
                print()
                Print ("deleting%s information succeeded!" "% name_ del)
                print()
                break
        else:
            print()
            Print ("the student name you entered is incorrect, please re-enter it")
            print()
    #Modify student
    def xiugai(self):
        name_ Xiugai=input ("please enter the name of the student you want to modify:")


        for student_dict_1 in self.student_list:

            if name_xiugai == student_dict_1["name"]:
                print()
                Print ("----- modify interface -----")
                print()
                Print ("name \t\t", "class \t\t", "age \t\t", "telephone number \t\t", "home address \t\t")
                print("%s\t\t%s\t\t%s\t\t%s\t\t%s" %(student_dict_1["name"],
                                                     student_dict_1["class"],
                                                     student_dict_1["age"],
                                                     student_dict_1["phone"],
                                                     student_dict_1["address"]))
                #Enter and do not modify

                student_ dict_ 1["name"]=self. new_ Input (student_dict_1["name"], "please enter the modified student name [enter without modification]:")
                student_ dict_ 1["class"]=self. new_ Input (student_dict_1["class"], "please enter the modified student class [enter without modifying]:")
                student_ dict_ 1["age"]=self. new_ Input (student_dict_1["age"], "please enter the modified student age [enter without modification]:")
                student_ dict_ 1["phone"]=self. new_ Input (student_dict_1["phone"], "please enter the modified student mobile number [enter without modification]:")
                student_ dict_ 1["address"]=self. new_ Input (student_dict_1["address"], "please enter the modified student's home address [enter without modifying]:")
                print()
                Print ("modification succeeded!")
                print()
                break
        else:
            print()
            Print ("the student name you entered is incorrect, please re-enter it")
            print()

    #Find students
    def find(self):
        find_ Name=input ("please enter the name of the student to be searched:")
        for student_dict_1 in self.student_list:

            if find_name == student_dict_1["name"]:
                print()
                Print ("----- query result interface -----")
                print()
                Print ("name \t\t", "class \t\t", "age \t\t", "telephone number \t\t", "home address \t\t")
                print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (student_dict_1["name"],
                                                      student_dict_1["class"],
                                                      student_dict_1["age"],
                                                      student_dict_1["phone"],
                                                      student_dict_1["address"]))
            else:
                print()
                Print ("----- query result interface -----")
                print()
                Print ("no such student information")

    #Show all student information
    def showall(self):

        if len(self.student_list)>0:
            print()
            Print ("----- display all student information -----")
            print()
            Print ("name \t\t", "class \t\t", "age \t\t", "telephone number \t\t", "home address \t\t")
            for student_dict_1 in self.student_list:

                print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (student_dict_1["name"],
                                                      student_dict_1["class"],
                                                      student_dict_1["age"],
                                                      student_dict_1["phone"],
                                                      student_dict_1["address"]))
        else:
            print()
            Print ("no data yet!")
            print()
    #Set the user to return the original value without input content, and return the new content with input content
    def new_input(self,yuanzhi,message):
        self.input_str=input(message)

        if len(self.input_str)>0:
            return self.input_str
        else:
            return yuanzhi


    #Save data to file
    def save_file(self):

        f = open("student2.txt", 'w', encoding='utf-8')
        f.write(str(self.student_list))
        f.close()
        Print ("data saved to student1.txt file successfully!")


    #Read data into variables
    def read_file(self):

         if os.path.exists('student2.txt'):
            f = open('student2.txt', 'r', encoding='utf-8')
            ret = f.read()

            self.student_list=eval(ret)
            f.close()
            Print ("data read succeeded!")

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepper.