Home > Backend Development > Python Tutorial > Introducing the Python object-oriented version of the student management system

Introducing the Python object-oriented version of the student management system

coldplay.xixi
Release: 2021-02-25 10:10:51
forward
2115 people have browsed it

Introducing the Python object-oriented version of the student management system

Free learning recommendation: python video tutorial

Article Directory

  • Python object-oriented version student management system
  • Objective
  • 1. System requirements
  • 2. Prepare program files
    • 2.1 Analysis
    • 2.2 Create program files
  • 3. Write program
    • 3.1 student.py
      • ##3.1.2 Program code
    • 3.2 managerSystem.py
      • 3.2.1 Define class
      • 3.2.2 Management system framework
    • 3.3 main.py
    • 3.4 Define system Function function
      • 3.4.1 Add function
      • 3.4.2 Delete student
      • 3.4.3 Modify student information
      • 3.4. 5 Query student information
      • 3.4.6 Display all student information
      • 3.4.7 Save student information
      • 3.4.8 Load student information
  • 4. Summary

Objective

    Understand the analysis method of internal functions of classes in the object-oriented development process
  • Understand common system functions
    • Add
    • Delete
    • Modify
    • Query

1. System requirements

Use object-oriented programming ideas to complete the development of the student management system, as follows:

    System requirements: Student data is stored in files中
  • System functions: add students, delete students, modify student information, query student information, display all student information, save student information and exit the system, etc.

2. Prepare program files

2.1 Analysis

    Character analysis
    • Student
    • Management System
Notes on work

    In order to facilitate the maintenance of the code, there is usually one role per role Program file;
  1. The project must have a main program entry, which is usually
  2. main.py
##2.2 Create program file

Create the project directory, for example:

StudentManagerSystem

The program file is as follows:

Program entry file: main.py
  • Student file: student.py
  • Management system file: managerSystem.py
3. Writing program

3.1 student. py

Requirements:

Student information includes: name, gender, mobile phone number;
  • Add
  • __str__
  • magic method, convenient View student object information
3.1.2 Program code

class Student(object):
    def __init__(self, name, gender, tel):
        self.name = name
        self.gender = gender
        self.tel = tel    def __str__(self):
        return f'{self.name}, {self.gender}, {self.tel}'
Copy after login

3.2 managerSystem.py

Requirements:

Location where data is stored: file (student.data)
  • Load file data
    • Modify the data and save it to the file
    Form of data storage: list storage student object
  • System function
  • Add student
    • Delete student
    • Modify student
    • Query Student information
    • Display all student information
    • Save student information
    • Exit the system
  • ##3.2.1 Definition class

class StudentManager(object):
    def __init__(self):
        # 存储数据所用的列表
        self.student_list = []
Copy after login
3.2.2 Management system framework

Requirements: System functions are used cyclically, and users enter different function numbers to perform different functions.

Steps

    Define program entry function
    • Load data
      • Display function menu
      • User input function number
      • Perform different functions according to the function number entered by the user
      • Define system function functions, add, delete students, etc.
    class StudentManager(object):
        def __init__(self):
            # 存储数据所用的列表
            self.student_list = []
    
        # 一. 程序入口函数,启动程序后执行的函数
        def run(self):
            # 1. 加载学员信息
            self.load_student()
    
            while True:
                # 2. 显示功能菜单
                self.show_menu()
    
                # 3. 用户输入功能序号
                menu_num = int(input('请输入您需要的功能序号:'))
    
                # 4 根据用户输入的功能序号执行不同的功能
                if menu_num == 1:
                    # 添加学员
                    self.add_student()
                elif menu_num == 2:
                    # 删除学员
                    self.del_student()
                elif menu_num == 3:
                    # 修改学员信息
                    self.modify_student()
                elif menu_num == 4:
                    # 查询学员信息
                    self.search_student()
                elif menu_num == 5:
                    # 显示所有学员信息
                    self.show_student()
                elif menu_num == 6:
                    # 保存学员信息
                    self.save_student()
                elif menu_num == 7:
                    # 退出系统
                    break
    
        # 二. 定义功能函数
        # 2.1 显示功能菜单
        @staticmethod
        def show_menu():
            print('请选择如下功能-----------------')
            print('1:添加学员')
            print('2:删除学员')
            print('3:修改学员信息')
            print('4:查询学员信息')
            print('5:显示所有学员信息')
            print('6:保存学员信息')
            print('7:退出系统')
    
        # 2.2 添加学员
        def add_student(self):
            pass
    
        # 2.3 删除学员
        def del_student(self):
            pass
    
        # 2.4 修改学员信息
        def modify_student(self):
            pass
    
        # 2.5 查询学员信息
        def search_student(self):
            pass
    
        # 2.6 显示所有学员信息
        def show_student(self):
            pass
    
        # 2.7 保存学员信息
        def save_student(self):
            pass
    
        # 2.8 加载学员信息
        def load_student(self):
            pass
    Copy after login
3.3 main.py

# 1. 导入managerSystem模块from managerSystem import *# 2. 启动学员管理系统if __name__ == '__main__':
    student_manager = StudentManager()

    student_manager.run()
Copy after login
3.4 Define system function function

3.4.1 Add function

Requirements: The user enters the student's name, gender, and mobile phone number to add the student to the system.
  • Steps
  • User input name, gender, mobile phone number

      Create the student object
    • Create the student object Add to list
    Code
  • # 添加学员函数内部需要创建学员对象,故先导入student模块from student import *class StudentManager(object):
    		......
        
        # 2.2 添加学员
        def add_student(self):
            # 1. 用户输入姓名、性别、手机号
            name = input('请输入您的姓名:')
            gender = input('请输入您的性别:')
            tel = input('请输入您的手机号:')
    
            # 2. 创建学员对象:先导入学员模块,再创建对象
            student = Student(name, gender, tel)
    
            # 3. 将该学员对象添加到列表
            self.student_list.append(student)
            
            # 打印信息
            print(self.student_list)
            print(student)
    Copy after login
  • 3.4.2 Delete student

Requirement: The user enters the name of the target student, and if the student exists, delete the student.

    Steps
  • The user enters the name of the target student
    • Traverse the student data list. If the student name entered by the user exists, delete it. Otherwise, it will prompt that the student does not exist.
    • Code
    # 2.3 删除学员:删除指定姓名的学员
    def del_student(self):
        # 1. 用户输入目标学员姓名
        del_name = input('请输入要删除的学员姓名:')
        
        # 2. 如果用户输入的目标学员存在则删除,否则提示学员不存在
        for i in self.student_list:
            if i.name == del_name:
                self.student_list.remove(i)
                break
        else:
            print('查无此人!')

        # 打印学员列表,验证删除功能
        print(self.student_list)
Copy after login
  • 3.4.3 Modify student information

    Requirements: User input target student Name, if the student exists, modify the student information.

      Steps
    • The user enters the name of the target student;
      • Traverse the student data list, and if the student name entered by the user exists, modify the student's name, gender, and mobile phone number data, otherwise It will prompt that the student does not exist.
      • Code
        # 2.4 修改学员信息
        def modify_student(self):
            # 1. 用户输入目标学员姓名
            modify_name = input('请输入要修改的学员的姓名:')
            # 2. 如果用户输入的目标学员存在则修改姓名、性别、手机号等数据,否则提示学员不存在
            for i in self.student_list:
                if i.name == modify_name:
                    i.name = input('请输入学员姓名:')
                    i.gender = input('请输入学员性别:')
                    i.tel = input('请输入学员手机号:')
                    print(f'修改该学员信息成功,姓名{i.name},性别{i.gender}, 手机号{i.tel}')
                    break
            else:
                print('查无此人!')
    Copy after login
  • 3.4.5 Query student information

    • 需求:用户输入目标学员姓名,如果学员存在则打印该学员信息
    • 步骤
      • 用户输入目标学员姓名
      • 遍历学员数据列表,如果用户输入的学员姓名存在则打印学员信息,否则提示该学员不存在。
    • 代码
        # 2.5 查询学员信息
        def search_student(self):
            # 1. 用户输入目标学员姓名
            search_name = input('请输入要查询的学员的姓名:')
    
            # 2. 如果用户输入的目标学员存在,则打印学员信息,否则提示学员不存在
            for i in self.student_list:
                if i.name == search_name:
                    print(f'姓名{i.name},性别{i.gender}, 手机号{i.tel}')
                    break
            else:
                print('查无此人!')
    Copy after login

    3.4.6 显示所有学员信息

    • 打印所有学员信息
    • 步骤
      • 遍历学员数据列表,打印所有学员信息
    • 代码
        # 2.6 显示所有学员信息
        def show_student(self):
            print('姓名\t性别\t手机号')
            for i in self.student_list:
                print(f'{i.name}\t{i.gender}\t{i.tel}')
    Copy after login

    3.4.7 保存学员信息

    • 需求:将修改后的学员数据保存到存储数据的文件。
    • 步骤
      • 打开文件
      • 文件写入数据
      • 关闭文件

    思考

    1. 文件写入的数据是学员对象的内存地址吗?
    2. 文件内数据要求的数据类型是什么?
    • 拓展__dict__
    class A(object):
        a = 0
    
        def __init__(self):
            self.b = 1aa = A()# 返回类内部所有属性和方法对应的字典print(A.__dict__)# 返回实例属性和值组成的字典print(aa.__dict__)
    Copy after login

    在Python中

    • 代码
        # 2.7 保存学员信息
        def save_student(self):
            # 1. 打开文件
            f = open('student.data', 'w')
    
            # 2. 文件写入学员数据
            # 注意1:文件写入的数据不能是学员对象的内存地址,需要把学员数据转换成列表字典数据再做存储
            new_list = [i.__dict__ for i in self.student_list]
            # [{'name': 'aa', 'gender': 'nv', 'tel': '111'}]
            print(new_list)
    
            # 注意2:文件内数据要求为字符串类型,故需要先转换数据类型为字符串才能文件写入数据
            f.write(str(new_list))
    
            # 3. 关闭文件
            f.close()
    Copy after login

    3.4.8 加载学员信息

    • 需求:每次进入系统后,修改的数据是文件里面的数据

    • 步骤

      • 尝试以"r"模式打开学员数据文件,如果文件不存在则以"w"模式打开文件
      • 如果文件存在则读取数据并存储数据
        • 读取数据
        • 转换数据类型为列表并转换列表内的字典为对象
        • 存储学员数据到学员列表
      • 关闭文件
    • 代码

        # 2.8 加载学员信息
        def load_student(self):
            # 尝试以"r"模式打开数据文件,文件不存在则提示用户;文件存在(没有异常)则读取数据
            try:
                f = open('student.data', 'r')
            except:
                f = open('student.data', 'w')
            else:
                # 1. 读取数据
                data = f.read()
    
                # 2. 文件中读取的数据都是字符串且字符串内部为字典数据,故需要转换数据类型再转换字典为对象后存储到学员列表
                new_list = eval(data)
                self.student_list = [Student(i['name'], i['gender'], i['tel']) for i in new_list]
            finally:
                # 3. 关闭文件
                f.close()
    Copy after login

    四. 总结

    • 函数
      • 定义和调用
      • 参数的使用
    • 面向对象
      • 定义类
      • 创建对象
      • 定义和调用实例属性
      • 定义和调用实例方法
    • 数据类型
      • 列表
        • 增加删除数据
        • 列表推导式
      • 字典
      • 字符串
    • 文件操作
      • 打开文件
      • 读取或写入
      • 关闭文件

    相关免费学习推荐:python教程(视频)

    The above is the detailed content of Introducing the Python object-oriented version of the student management system. For more information, please follow other related articles on the PHP Chinese website!

  • Related labels:
    source:csdn.net
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template