news 2026/3/1 21:25:48

Python 学生管理系统实战:从基础功能到数据持久化(附完整源码)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python 学生管理系统实战:从基础功能到数据持久化(附完整源码)

学生管理系统基础功能实现

学生管理系统的核心功能包括添加、删除、修改和查询学生信息。使用Python内置数据结构如字典和列表可以快速实现这些基础功能。

students = [] def add_student(): name = input("输入学生姓名: ") age = int(input("输入学生年龄: ")) student_id = input("输入学号: ") students.append({"name": name, "age": age, "id": student_id}) def show_students(): for student in students: print(f"姓名: {student['name']}, 年龄: {student['age']}, 学号: {student['id']}") def delete_student(): student_id = input("输入要删除学生的学号: ") global students students = [s for s in students if s['id'] != student_id]

数据持久化存储方案

使用JSON文件格式可以实现数据的持久化存储,确保程序关闭后数据不会丢失。Python的json模块提供了简单易用的接口。

import json def save_data(): with open('students.json', 'w') as f: json.dump(students, f) def load_data(): global students try: with open('students.json', 'r') as f: students = json.load(f) except FileNotFoundError: students = []

用户界面与交互设计

构建简单的命令行交互界面,通过while循环和条件判断实现功能选择。清晰的菜单提示能提升用户体验。

def main_menu(): print("\n学生管理系统") print("1. 添加学生") print("2. 显示所有学生") print("3. 删除学生") print("4. 退出系统") def run_system(): load_data() while True: main_menu() choice = input("请选择操作(1-4): ") if choice == '1': add_student() elif choice == '2': show_students() elif choice == '3': delete_student() elif choice == '4': save_data() break

系统功能扩展建议

考虑添加成绩管理模块,为每个学生增加科目和分数记录。可以实现成绩统计和分析功能,如计算平均分和排名。

def add_score(): student_id = input("输入学生学号: ") subject = input("输入科目名称: ") score = float(input("输入分数: ")) for student in students: if student['id'] == student_id: if 'scores' not in student: student['scores'] = {} student['scores'][subject] = score break

异常处理与输入验证

增强系统的健壮性需要添加输入验证和异常处理机制,防止无效输入导致程序崩溃。

def get_int_input(prompt): while True: try: return int(input(prompt)) except ValueError: print("请输入有效的数字!")

https://www.zhihu.com/zvideo/1994901640575214623/
https://www.zhihu.com/zvideo/1994901639455332312/
https://www.zhihu.com/zvideo/1994901639463732153/
https://www.zhihu.com/zvideo/1994901638213817222/
https://www.zhihu.com/zvideo/1994901636422866760/
https://www.zhihu.com/zvideo/1994901635860803870/
https://www.zhihu.com/zvideo/1994901635223279332/
https://www.zhihu.com/zvideo/1994901635093247206/
https://www.zhihu.com/zvideo/1994901634782892976/
https://www.zhihu.com/zvideo/1994901633855922370/
https://www.zhihu.com/zvideo/1994901633839146142/
https://www.zhihu.com/zvideo/1994901633147086093/
https://www.zhihu.com/zvideo/1994901632165622682/
https://www.zhihu.com/zvideo/1994901632312435743/
https://www.zhihu.com/zvideo/1994901631456790443/
https://www.zhihu.com/zvideo/1994901628684353568/
https://www.zhihu.com/zvideo/1994901628046815323/
https://www.zhihu.com/zvideo/1994901628222972059/
https://www.zhihu.com/zvideo/1994901625840616672/
https://www.zhihu.com/zvideo/1994901625089839369/
https://www.zhihu.com/zvideo/1994901624246789427/
https://www.zhihu.com/zvideo/1994901623433086823/
https://www.zhihu.com/zvideo/1994901622032205640/
https://www.zhihu.com/zvideo/1994901619217806544/
https://www.zhihu.com/zvideo/1994901617984700436/
https://www.zhihu.com/zvideo/1994901617535901863/
https://www.zhihu.com/zvideo/1994901614365017076/
https://www.zhihu.com/zvideo/1994901615635877935/
https://www.zhihu.com/zvideo/1994901613178003651/
https://www.zhihu.com/zvideo/1994901611789710962/
https://www.zhihu.com/zvideo/1994901611139572034/
https://www.zhihu.com/zvideo/1994901607482151535/
https://www.zhihu.com/zvideo/1994901608488788992/
https://www.zhihu.com/zvideo/1994901608060970783/
https://www.zhihu.com/zvideo/1994901606672660197/
https://www.zhihu.com/zvideo/1994901606966256910/
https://www.zhihu.com/zvideo/1994901605800236919/
https://www.zhihu.com/zvideo/1994901605355648384/
https://www.zhihu.com/zvideo/1994901604470649566/
https://www.zhihu.com/zvideo/1994901603011027800/
https://www.zhihu.com/zvideo/1994901602218308616/
https://www.zhihu.com/zvideo/1994901600947434847/
https://www.zhihu.com/zvideo/1994901599416497346/
https://www.zhihu.com/zvideo/1994901599039025393/
https://www.zhihu.com/zvideo/1994901597323539244/
https://www.zhihu.com/zvideo/1994901598573445203/
https://www.zhihu.com/zvideo/1994901598397288520/
https://www.zhihu.com/zvideo/1994901596547613356/
https://www.zhihu.com/zvideo/1994901596467905733/
https://www.zhihu.com/zvideo/1994901596677640694/

完整系统源码结构

一个完整的学生管理系统应该包含以下模块:

  • main.py: 程序入口和主循环
  • student.py: 学生数据模型和操作
  • storage.py: 数据持久化处理
  • ui.py: 用户界面和交互逻辑
# main.py示例 from storage import load_data, save_data from ui import run_system if __name__ == "__main__": load_data() run_system() save_data()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/1 2:04:00

go语言对phone脱敏显示

在Go语言中实现手机号脱敏显示主要有以下几种方式,从简单到完整逐步推荐: 一、基础实现(字符串切片) 最常用且高效的方式是直接使用字符串切片操作,保留前3位和后4位,中间用*替换: go 复制 …

作者头像 李华
网站建设 2026/2/25 22:55:08

开源模型新选择:AnimeGANv2宫崎骏风格迁移实战指南

开源模型新选择:AnimeGANv2宫崎骏风格迁移实战指南 1. 引言 随着深度学习在图像生成领域的持续突破,风格迁移技术已从实验室走向大众应用。其中,AnimeGANv2 作为轻量级、高效率的动漫风格迁移模型,凭借其出色的画质表现和极低的…

作者头像 李华
网站建设 2026/2/23 14:51:36

Windows 10/11 优化大师 Windows Manager

一、前言:为什么 Windows 10/11 越用越卡? 相信很多朋友都有这样的体验: 新装的 Windows 10 / Windows 11 用着很流畅用了一段时间后: 开机变慢系统反应迟钝后台服务越来越多磁盘空间被莫名其妙占满 即便你不安装乱七八糟的软…

作者头像 李华
网站建设 2026/3/1 3:00:00

VibeThinker-1.5B-WEBUI权限管理:多用户场景下的配置建议

VibeThinker-1.5B-WEBUI权限管理:多用户场景下的配置建议 1. 引言 1.1 业务场景描述 随着轻量级大模型在开发者社区的广泛应用,VibeThinker-1.5B-WEBUI 因其低成本、高推理效率的特点,逐渐成为个人开发者和小型团队进行数学推导与编程辅助…

作者头像 李华
网站建设 2026/2/27 22:08:40

【性能测试】2_JMeter _JMeter文件目录

文章目录一、Bin目录二、docs目录三、printable_docs目录四、lib目录一、Bin目录 Bin目录:存放可执行文件和配置文件。 examples:目录下包含Jmeter使用实例ApacheJMeter.jar:JMeter源码包jmeter.bat:windows下启动文件jmeter.sh&#xff1a…

作者头像 李华
网站建设 2026/2/24 19:27:45

计算机毕设 java 基于 java 青少年篮球俱乐部管理系统设计与实现 基于 Java 的青少年篮球俱乐部管理平台 赛事组织与运动员服务系统

计算机毕设 java 基于 java 青少年篮球俱乐部管理系统设计与实现(配套有源码、程序、MySQL 数据库、论文),本套源码可先查看功能演示视频,文末有联系方式可领取。传统青少年篮球俱乐部管理存在赛事信息传递不及时、报名流程繁琐、…

作者头像 李华