教你用Python实现简易版学生信息管理系统(含源码)

yizhihongxing

教你用Python实现简易版学生信息管理系统(含源码)

概述

本文将介绍如何使用 Python 编写一个简单的学生信息管理系统。本系统支持添加、查询、删除和修改学生信息,并且所有数据都存储在本地文本文件中。本文将详细介绍系统的实现流程,并提供完整的源码。

实现步骤

1. 创建项目

首先,在本地环境中创建一个新的 Python 项目文件夹,并在文件夹中创建一个名为 students.txt 的空文本文件。该文件将用于存储所有学生的信息。

2. 实现学生类

创建一个名为 Student 的 Python 类,该类具有以下属性:idnamegenderagescore。通过实现该类,我们可以方便地对学生信息进行管理。

示例代码:

class Student:
    def __init__(self, id, name, gender, age, score):
        self.id = id
        self.name = name
        self.gender = gender
        self.age = age
        self.score = score

3. 实现数据存储工具类

创建一个名为 DataTool 的 Python 类,该类具有以下方法:

  • read_data():从本地文本文件中读取所有学生的信息,并将其存储在列表中。
  • write_data(data_list):将传递的学生信息列表写入本地文本文件。

示例代码:

class DataTool:
    def __init__(self, file_path):
        self.file_path = file_path

    def read_data(self):
        with open(self.file_path, 'r') as f:
            content = f.readlines()

        student_list = []
        for item in content:
            item = item.strip().split(',')
            student_list.append(Student(item[0], item[1], item[2], item[3], item[4]))
        return student_list

    def write_data(self, data_list):
        with open(self.file_path, 'w') as f:
            for item in data_list:
                write_str = '{0},{1},{2},{3},{4}\n'.format(item.id, item.name, item.gender, item.age, item.score)
                f.write(write_str)

4. 实现学生信息管理类

创建一个名为 StudentManager 的 Python 类,该类具有以下方法:

  • show_all_student():打印出所有学生的信息。
  • add_student():向学生列表中添加一个新学生。
  • delete_student():从学生列表中删除指定学生。
  • update_student():更新指定学生的信息。

示例代码:

class StudentManager:
    def __init__(self, file_path):
        self.data_tool = DataTool(file_path)
        self.student_list = self.data_tool.read_data()

    def show_all_student(self):
        if not self.student_list:
            print('No student information available.\n')
            return
        print('id  name  gender  age  score')
        for item in self.student_list:
            print('{0}  {1}  {2}  {3}  {4}'.format(item.id, item.name, item.gender, item.age, item.score))
        print()

    def add_student(self):
        id = input('Please enter the student id: ')
        name = input('Please enter the name of the student: ')
        gender = input('Please enter the gender of the student: ')
        age = input('Please enter the age of the student: ')
        score = input('Please enter the score of the student: ')

        new_student = Student(id, name, gender, age, score)
        self.student_list.append(new_student)
        self.data_tool.write_data(self.student_list)
        print('Add student successfully!\n')

    def delete_student(self):
        delete_id = input('Please enter the student id you want to delete: ')
        for i in range(len(self.student_list)):
            if self.student_list[i].id == delete_id:
                del self.student_list[i]
                self.data_tool.write_data(self.student_list)
                print('Delete student successfully!\n')
                return
        else:
            print('Sorry, student id not found.\n')

    def update_student(self):
        update_id = input('Please enter the student id you want to update: ')
        for item in self.student_list:
            if item.id == update_id:
                item.name = input('Please enter the new name of the student: ')
                item.gender = input('Please enter the new gender of the student: ')
                item.age = input('Please enter the new age of the student: ')
                item.score = input('Please enter the new score of the student: ')
                self.data_tool.write_data(self.student_list)
                print('Update student information successfully!\n')
                return
        else:
            print('Sorry, student id not found.\n')

5. 实现入口函数

创建一个名为 main() 的 Python 函数,用于运行该程序。通过调用 StudentManager 类中的方法,可以方便地实现对学生信息的管理。

示例代码:

def main():
    file_path = './students.txt'
    student_manager = StudentManager(file_path)

    while True:
        print('=== Student Information Management System ===')
        print('1. Show all student information.')
        print('2. Add new student.')
        print('3. Delete student.')
        print('4. Update student information.')
        print('0. Quit system.')
        choice = input('Please make a choice: ')

        if choice == '1':
            student_manager.show_all_student()

        elif choice == '2':
            student_manager.add_student()

        elif choice == '3':
            student_manager.delete_student()

        elif choice == '4':
            student_manager.update_student()

        elif choice == '0':
            print('Thank you for using Student Information Management System, goodbye!')
            break

        else:
            print('Invalid input, please try again.\n')

程序演示

请参考下方两个示例:

示例 1:添加学生信息

=== Student Information Management System ===
1. Show all student information.
2. Add new student.
3. Delete student.
4. Update student information.
0. Quit system.
Please make a choice: 2

Please enter the student id: 001
Please enter the name of the student: John
Please enter the gender of the student: Male
Please enter the age of the student: 18
Please enter the score of the student: 90

Add student successfully!

示例 2:显示所有学生信息

=== Student Information Management System ===
1. Show all student information.
2. Add new student.
3. Delete student.
4. Update student information.
0. Quit system.
Please make a choice: 1

id  name  gender  age  score
001  John  Male  18  90

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:教你用Python实现简易版学生信息管理系统(含源码) - Python技术站

(0)
上一篇 2023年5月30日
下一篇 2023年5月30日

相关文章

  • python with statement 进行文件操作指南

    下面是详细讲解“Python with语句进行文件操作指南”的完整攻略。 前置知识 在讲解”Python with语句进行文件操作指南”之前,需要掌握以下基础知识。 with语句 with语句用于处理资源(文件、网络连接、等)的分配和释放,它可以保证在任何情况下,使用完资源后都能正确地释放资源。 语法: with 资源变量 as 目标变量: # 使用资源的代…

    python 2023年6月2日
    00
  • 一行Python代码过滤标点符号等特殊字符

    在 Python 中,我们可以使用正则表达式来过滤标点符号等特殊字符。以下是一行 Python 代码,可以过滤掉字符串中的标点符号等特殊字符: import re text = "Hello, world! This is a test string." filtered_text = re.sub(r'[^\w\s]’, ”, tex…

    python 2023年5月14日
    00
  • Python restful框架接口开发实现

    Python是一种广泛使用的高级编程语言,在Web开发领域中应用广泛。当我们需要使用Python开发RESTful API时,可以使用Python的RESTful框架来简化开发流程,提高开发效率。下面是Python RESTful框架接口开发实现的完整攻略。 准备工作 在开始Python RESTful框架接口开发实现之前,需要进行一些准备工作。具体如下: …

    python 2023年5月13日
    00
  • Python命令行参数解析工具 docopt 安装和应用过程详解

    Python命令行参数解析工具 docopt 安装和应用过程详解 什么是 docopt docopt 是 Python 的一个命令行参数解析库,其最大的特点在于使用一份帮助文档(docstring)来定义命令行接口,而不是像传统的 argparse、getopt 这些工具一样需要手动编写参数列表、参数解析规则、帮助信息等。docopt 的官方网站有更详细的介…

    python 2023年6月3日
    00
  • 如何在Python中删除SQLite数据库中的数据?

    以下是在Python中删除SQLite数据库中的数据的完整使用攻略。 删除SQLite数据库中的数据简介 在Python中,可以使用sqlite3模块连接SQLite数据库,并使用DELETE FROM语句删除数据。删除数据时,需要指定要删除的表和删除条件。删除结果可以使用游标对象的rowcount属性获取。 步骤1:导入模块 在Python中,使用sqli…

    python 2023年5月12日
    00
  • Python实现图像随机添加椒盐噪声和高斯噪声

    针对题目“Python实现图像随机添加椒盐噪声和高斯噪声”的完整攻略,可以分为以下步骤: 第一步:读取图像并进行预处理 在Python中,可以使用OpenCV库来读取图像,并对图像进行预处理,如调整图像大小、改变图像颜色空间等。在读取图像时,建议采用灰度图像读取方式,便于后续噪声生成和处理。示例代码如下: import cv2 # 读取灰度图像 img = …

    python 2023年6月3日
    00
  • python 模块重载的五种方法

    Python 模块重载的五种方法 在Python中,我们可以通过定义模块来将代码分离成不同的文件,方便管理和维护。但是在开发过程中,我们经常需要对模块进行修改,然后重新加载模块,这时候就需要用到模块重载了。本文将介绍 Python 模块重载的五种方法。 方法一:使用 reload() 函数 最基本的模块重载方法就是使用内置函数 reload()。通过调用 r…

    python 2023年6月2日
    00
  • 五个有趣的Python整蛊小程序合集

    下面是对“五个有趣的Python整蛊小程序合集”完整攻略的详细讲解: 一、项目简介 本项目是一个有趣的Python整蛊小程序合集,包含了五个小程序。这些小程序通过代码的方式制造一些有趣的效果,例如变换鼠标指针样式、模拟键盘输入等。这些可以用于整蛊朋友或者在自己的电脑上玩一些小九九。 以下是五个小程序的简介:1. 短网址-恶搞器:将输入的网址转换为一个极短的伪…

    python 2023年5月23日
    00
合作推广
合作推广
分享本页
返回顶部