python代码实现学生信息管理系统
作者:"Blue 时间:2023-02-06 18:31:34
本文实例为大家分享了python实现学生信息管理系统的具体代码,含代码注释、增删改查、排序、统计显示学生信息,供大家参考,具体内容如下
运行如下:
具体代码如下:
# _*_ coding:utf-8 _*_
import re # 导入正则表达式模块
import os # 导入操作系统模块
filename = "students.txt" # 定义保存学生信息的文件名
def menu():
print("------------------------学生管理系统------------------------")
print('\t\t\t1.录入学生信息')
print('\t\t\t2.查找学生信息')
print('\t\t\t3.删除学生信息')
print('\t\t\t4.修改学生信息')
print('\t\t\t5.排序')
print('\t\t\t6.统计学生总人数')
print('\t\t\t7.显示所有学生信息')
print('\t\t\t0.退出')
print('------------------------------------------------------------')
def main():
ctrl = True # 标记是否退出系统
while (ctrl):
menu() # 显示菜单
option = input("请选择:") # 选择菜单项
option_str = re.sub("\D", "", option) # 提取数字
if option_str in ['0', '1', '2', '3', '4', '5', '6', '7']:
option_int = int(option_str)
if option_int == 0: # 退出系统
print('您已退出学生成绩管理系统!')
ctrl = False
elif option_int == 1: # 录入学生成绩信息
insert()
elif option_int == 2: # 查找学生成绩信息
search()
elif option_int == 3: # 删除学生成绩信息
delete()
elif option_int == 4: # 修改学生成绩信息
modify()
elif option_int == 5: # 排序
sort()
elif option_int == 6: # 统计学生总数
total()
elif option_int == 7: # 显示所有学生信息
show()
'''1 录入学生信息'''
def insert():
stdentList = [] # 保存学生信息的列表
mark = True # 是否继续添加
while mark:
id = input("请输入ID(如 1001):")
if not id: # ID为空,跳出循环
break
name = input("请输入名字:")
if not name: # 名字为空,跳出循环
break
try:
english = int(input("请输入英语成绩:"))
python = int(input("请输入Python成绩:"))
c = int(input("请输入C语言成绩:"))
except:
print("输入无效,不是整型数值....重新录入信息")
continue
stdent = {"id": id, "name": name, "english": english, "python": python, "c": c} # 将输入的学生信息保存到字典
stdentList.append(stdent) # 将学生字典添加到列表中
inputMark = input("是否继续添加?(y/n):")
if inputMark == "y": # 继续添加
mark = True
else: # 不继续添加
mark = False
save(stdentList) # 将学生信息保存到文件
print("学生信息录入完毕!!!")
# 将学生信息保存到文件
def save(student):
try:
students_txt = open(filename, "a") # 以追加模式打开
except Exception as e:
students_txt = open(filename, "w") # 文件不存在,创建文件并打开
for info in student:
students_txt.write(str(info) + "\n") # 按行存储,添加换行符
students_txt.close() # 关闭文件
'''2 查找学生成绩信息'''
def search():
mark = True
student_query = [] # 保存查询结果的学生列表
while mark:
id = ""
name = ""
if os.path.exists(filename): # 判断文件是否存在
mode = input("按ID查输入1;按姓名查输入2:")
if mode == "1":
id = input("请输入学生ID:")
elif mode == "2":
name = input("请输入学生姓名:")
else:
print("您的输入有误,请重新输入!")
search() # 重新查询
with open(filename, 'r') as file: # 打开文件
student = file.readlines() # 读取全部内容
for list in student:
d = dict(eval(list)) # 字符串转字典
if id is not "": # 判断是否按ID查
if d['id'] == id:
student_query.append(d) # 将找到的学生信息保存到列表中
elif name is not "": # 判断是否按姓名查
if d['name'] == name:
student_query.append(d) # 将找到的学生信息保存到列表中
show_student(student_query) # 显示查询结果
student_query.clear() # 清空列表
inputMark = input("是否继续查询?(y/n):")
if inputMark == "y":
mark = True
else:
mark = False
else:
print("暂未保存数据信息...")
return
'''3 删除学生成绩信息'''
def delete():
mark = True # 标记是否循环
while mark:
studentId = input("请输入要删除的学生ID:")
if studentId is not "": # 判断要删除的学生是否存在
if os.path.exists(filename): # 判断文件是否存在
with open(filename, 'r') as rfile: # 打开文件
student_old = rfile.readlines() # 读取全部内容
else:
student_old = []
ifdel = False # 标记是否删除
if student_old: # 如果存在学生信息
with open(filename, 'w') as wfile: # 以写方式打开文件
d = {} # 定义空字典
for list in student_old:
d = dict(eval(list)) # 字符串转字典
if d['id'] != studentId:
wfile.write(str(d) + "\n") # 将一条学生信息写入文件
else:
ifdel = True # 标记已经删除
if ifdel:
print("ID为 %s 的学生信息已经被删除..." % studentId)
else:
print("没有找到ID为 %s 的学生信息..." % studentId)
else: # 不存在学生信息
print("无学生信息...")
break # 退出循环
show() # 显示全部学生信息
inputMark = input("是否继续删除?(y/n):")
if inputMark == "y":
mark = True # 继续删除
else:
mark = False # 退出删除学生信息功能
'''4 修改学生成绩信息'''
def modify():
show() # 显示全部学生信息
if os.path.exists(filename): # 判断文件是否存在
with open(filename, 'r') as rfile: # 打开文件
student_old = rfile.readlines() # 读取全部内容
else:
return
studentid = input("请输入要修改的学生ID:")
with open(filename, "w") as wfile: # 以写模式打开文件
for student in student_old:
d = dict(eval(student)) # 字符串转字典
if d["id"] == studentid: # 是否为要修改的学生
print("找到了这名学生,可以修改他的信息!")
while True: # 输入要修改的信息
try:
d["name"] = input("请输入姓名:")
d["english"] = int(input("请输入英语成绩:"))
d["python"] = int(input("请输入Python成绩:"))
d["c"] = int(input("请输入C语言成绩:"))
except:
print("您的输入有误,请重新输入。")
else:
break # 跳出循环
student = str(d) # 将字典转换为字符串
wfile.write(student + "\n") # 将修改的信息写入到文件
print("修改成功!")
else:
wfile.write(student) # 将未修改的信息写入到文件
mark = input("是否继续修改其他学生信息?(y/n):")
if mark == "y":
modify() # 重新执行修改操作
'''5 排序'''
def sort():
show() # 显示全部学生信息
if os.path.exists(filename): # 判断文件是否存在
with open(filename, 'r') as file: # 打开文件
student_old = file.readlines() # 读取全部内容
student_new = []
for list in student_old:
d = dict(eval(list)) # 字符串转字典
student_new.append(d) # 将转换后的字典添加到列表中
else:
return
ascORdesc = input("请选择(0升序;1降序):")
if ascORdesc == "0": # 按升序排序
ascORdescBool = False # 标记变量,为False表示升序排序
elif ascORdesc == "1": # 按降序排序
ascORdescBool = True # 标记变量,为True表示降序排序
else:
print("您的输入有误,请重新输入!")
sort()
mode = input("请选择排序方式(1按英语成绩排序;2按Python成绩排序;3按C语言成绩排序;0按总成绩排序):")
if mode == "1": # 按英语成绩排序
student_new.sort(key=lambda x: x["english"], reverse=ascORdescBool)
elif mode == "2": # 按Python成绩排序
student_new.sort(key=lambda x: x["python"], reverse=ascORdescBool)
elif mode == "3": # 按C语言成绩排序
student_new.sort(key=lambda x: x["c"], reverse=ascORdescBool)
elif mode == "0": # 按总成绩排序
student_new.sort(key=lambda x: x["english"] + x["python"] + x["c"], reverse=ascORdescBool)
else:
print("您的输入有误,请重新输入!")
sort()
show_student(student_new) # 显示排序结果
''' 6 统计学生总数'''
def total():
if os.path.exists(filename): # 判断文件是否存在
with open(filename, 'r') as rfile: # 打开文件
student_old = rfile.readlines() # 读取全部内容
if student_old:
print("一共有 %d 名学生!" % len(student_old))
else:
print("还没有录入学生信息!")
else:
print("暂未保存数据信息...")
''' 7 显示所有学生信息 '''
def show():
student_new = []
if os.path.exists(filename): # 判断文件是否存在
with open(filename, 'r') as rfile: # 打开文件
student_old = rfile.readlines() # 读取全部内容
for list in student_old:
student_new.append(eval(list)) # 将找到的学生信息保存到列表中
if student_new:
show_student(student_new)
else:
print("暂未保存数据信息...")
# 将保存在列表中的学生信息显示出来
def show_student(studentList):
if not studentList:
print("(o@.@o) 无数据信息 (o@.@o) \n")
return
format_title = "{:^6}{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}"
print(format_title.format("ID", "名字", "英语成绩", "Python成绩", "C语言成绩", "总成绩"))
format_data = "{:^6}{:^12}\t{:^12}\t{:^12}\t{:^12}\t{:^12}"
for info in studentList:
print(format_data.format(info.get("id"), info.get("name"), str(info.get("english")), str(info.get("python")),
str(info.get("c")),
str(info.get("english") + info.get("python") + info.get("c")).center(12)))
if __name__ == "__main__":
main()
来源:https://blog.csdn.net/weixin_61370021/article/details/122987688
标签:python,信息,管理系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python传递参数的多种方式(小结)
2023-05-10 23:05:46
理解Sql Server中的聚集索引
2024-01-23 11:51:46
![](https://img.aspxhome.com/file/2023/3/91193_0s.png)
python 数据类型强制转换的总结
2021-03-25 03:40:26
使用python写一个自动浏览文章的脚本实例
2023-11-10 10:52:52
Python WSGI 规范简介
2022-02-06 23:30:06
![](https://img.aspxhome.com/file/2023/2/92222_0s.png)
defineProperty和Proxy基础功能及性能对比
2024-06-05 09:19:42
python根据开头和结尾字符串获取中间字符串的方法
2021-01-02 01:44:28
Python处理文本换行符实例代码
2021-08-19 18:31:51
![](https://img.aspxhome.com/file/2023/5/67225_0s.png)
OpenCV图像缩放之cv.resize()函数详解
2021-02-06 12:36:58
![](https://img.aspxhome.com/file/2023/7/93817_0s.jpg)
asp入门之字符串函数介绍示例
2008-11-04 20:18:00
django之导入并执行自定义的函数模块图解
2023-07-27 02:45:19
![](https://img.aspxhome.com/file/2023/0/86240_0s.jpg)
getdata table表格数据join mysql方法
2024-01-25 17:55:08
Python编程pygal绘图实例之XY线
2021-02-17 10:42:10
![](https://img.aspxhome.com/file/2023/2/84112_0s.png)
Vue3-KeepAlive,多个页面使用keepalive方式
2024-05-02 16:33:39
![](https://img.aspxhome.com/file/2023/7/130237_0s.gif)
解决pycharm安装第三方库失败的问题
2023-07-13 19:32:12
![](https://img.aspxhome.com/file/2023/8/120278_0s.jpg)
python使用正则表达式来获取文件名的前缀方法
2023-02-08 07:37:58
Python基于百度AI实现抓取表情包
2022-05-01 00:01:35
![](https://img.aspxhome.com/file/2023/3/83913_0s.jpg)
python字典排序浅析介绍
2022-01-30 17:45:00
![](https://img.aspxhome.com/file/2023/7/127577_0s.png)
JS组件Bootstrap实现图片轮播效果
2024-04-22 13:03:11
![](https://img.aspxhome.com/file/2023/0/135900_0s.gif)
Python3.4编程实现简单抓取爬虫功能示例
2022-04-23 17:48:15