为了巩固刚学习的pyhton基础语法,决定写一个简单的学生信息管理系统。
首先,学生信息管理嘛,我们需要学生和学生信息,所以我们封装一个学生类:
#存储学生信息的类
class Student:
"存储学生信息"
def __init__(self):
self.name = "name"
self.ID = "000"
self.score = 0
def Set(self, name, ID, score):
self.name = name
self.ID = ID
self.score = score
def GetName(self):
return self.name
def GetID(self):
return self.ID
def GetScore(self):
return self.score
def PrintStudent(self):
print("姓名:" + self.name)
print("学号:" + self.ID)
print("成绩:" + self.score)
存储信息的数据结构设计好了,接下来实现管理系统的基本功能,我们将这些部分封装为不同的函数,函数的参数为可变类型的列表: 增
#向列表中添加学生信息
def AddMSG(stu_list):
name = input("请输入要添加的学生姓名:")
ID = input("请输入要添加的学生学号:")
score = input("请输入要添加的学生成绩:")
temp_stu = Student()
temp_stu.Set(name, ID, score)
stu_list.append(temp_stu)
print("添加成功!")
删
#从列表中删除学生信息
def DelMSG(stu_list):
name = input("请输入要删除的学生姓名:")
is_find = False
for i in range(len(stu_list)):
if (stu_list[i].GetName() == name):
is_find = True
del stu_list[i]
print("删除成功!")
break
else:
pass
if is_find == False:
print("未找到该学生!")
查
#从列表中查找学生信息
def FindMSG(stu_list):
name = input("请输入要查找的学生姓名:")
is_find = False
for i in range(len(stu_list)):
if (stu_list[i].GetName() == name):
is_find = True
stu_list[i].PrintStudent()
break
else:
pass
if is_find == False:
print("未找到该学生!")
改
#修改学生信息
def UpdateMSG(stu_list):
name = input("请输入要修改的学生姓名:")
is_find = False
for i in range(len(stu_list)):
if (stu_list[i].GetName() == name):
is_find = True
name = input("修改姓名为:")
ID = input("修改学号为:")
score = input("修改成绩为:")
stu_list[i].Set(name, ID, score)
print("修改成功!")
break
else:
pass
if is_find == False:
print("未找到该学生!")
除了基本的 “增,删,查,改” ,我们再增加一个查看所有信息的函数:
#打印出所有学生信息
def ShowMSG(stu_list):
print("学生信息")
for i in range(len(stu_list)):
print(stu_list[i].GetName() + " " + stu_list[i].GetID() + " " + stu_list[i].GetScore())
功能实现好了,现在我们需要一个交互界面,一个简单的菜单如下:
#打印主菜单
def ShowMenu():
print("@学生管理系统@")
print("1.添加学生信息")
print("2.删除学生信息")
print("3.查询学生信息")
print("4.修改学生信息")
print("5.查看所有信息")
print("6.退出管理系统")
主程序需要的所有模块完成啦,将他们组装起来吧:
def Main():
student_list = []
ShowMenu()
switch = True
while switch:
key = input("请输入命令:")
if key == "1":
AddMSG(student_list)
elif key == "2":
DelMSG(student_list)
elif key == "3":
FindMSG(student_list)
elif key == "4":
UpdateMSG(student_list)
elif key == "5":
ShowMSG(student_list)
elif key == "6":
switch = False
else:
print("输入错误!")
到这里,小工告成啦,调用 Main 函数启动学生信息管理系统! 运行效果: