# 学生管理系统
import os
#定义学生类
class Student:
def __init__(self,id,name,age,score):
self.id = id
self.name = name
self.age = age
self.score = score
def __str__(self):
#msg = "{'id':" + "'"+self.id +"'"+ ",'name':" + "'" +self.name + "'" + ",'age':" + "'" + self.age+"'}"
msg = "学生信息:id=" + self.id + ",name=" + self.name + ",age=" + self.age + ",score=" + self.score
return msg
#获取id
def getId(self):
return self.id
#获取name
def getName(self):
return self.name
#获取age
def getAge(self):
return self.age
#获取Score
def getScore(self):
return self.score
#设置name
def setName(self,name):
self.name = name
#设置age
def setAge(self,age):
self.age = age
#设置分数
def setScore(self,score):
self.score = score
# 添加学生信息
def addStu(array):
"添加学生信息"
print("无输入可按回车结束录入!")
while True:
id = input("请输入学生学号:")
if not id:
break
try:
for i in range(len(array)):
stu2 = array[i]
if id == stu2.getId():
print("该学号已存在,不能重复添加")
return
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
score = input("请输入学生成绩:")
stu = Student(id,name,age,score) # 把单个学生添加到总列表中
array.append(stu)
print("添加成功:",stu)
except:
print("输入无效,不是整形数值....重新录入信息")
continue
print("学生信息录入完毕!!!")
# 删除学生信息
def delStu(array):
"删除学生信息"
id = input("请输入要删除的学生学号:")
for i in range(len(array)):
stu2 = array[i]
if id == stu2.getId():
del array[i]
return 0
return 1
# 修改学生信息
def updateStu(array):
"修改学生信息"
id = input("请输入要修改的学生学号:")
for i in range(len(array)):
stu2 = array[i]
if id == stu2.getId():
name = input("请输入要修改的学生姓名:")
age = input("请输入要修改的学生年龄:")
score = input("请输入要修改的学生分数:")
stu2.setName(name)
stu2.setAge(age)
stu2.setScore(score)
print("修改成功")
return
print("找不到该学号,没法修改")
# 查询学生信息
def selectStu(array):
"查询学生信息"
id = input("请输入要查询的学生学号:")
for i in range(len(array)):
stu2 = array[i]
if id == stu2.getId():
print("查询到的学生信息:",stu2)
return
print("查询失败,查不到该学生信息")
return
#打印学生信息
def printStuInfo(array):
for i in range(len(array)):
stu = array[i]
print(stu)
def meun():
menu_info = '''+----------------------+
| 1 )添加学生信息(输入名字时为空时结束添加)
| 2 )删除学生信息
| 3 )修改学生信息
| 4 )查询学生信息
| 5 )按学生成绩高-低显示学生信息
| 6 )按学生成绩低-高显示学生信息
| 7 )保存学生信息到文件(students.txt)
| 8 )从文件中读取数据(students.txt)
| 9 ) 退出系统
---------------------
'''
print(menu_info)
def save_info(array):
try:
filename = 'write_data.txt' #文件名
with open(filename, 'w') as f:
for i in range(len(array)):
if i == len(array)-1 :
stu =array[i]
f.write(stu.getId() + ","+stu.getName()+","+stu.getAge()+","+stu.getScore())
else:
stu = array[i]
f.write(stu.getId() + ","+stu.getName()+","+stu.getAge() +","+stu.getScore() +"\n")
f.close()
except Exception as e:
file = open(filename, "x") # 文件不存在,创建文件并打开
def read_info(array):
try:
filename = 'write_data.txt' #文件名
f=open(filename,"r")
content = f.readlines()# 整行读取
print("文件内容:",content)
array.extend(content) #用新列表拓展原来的列表
array_temp = [] # 临时变量
for i in range(len(array)): # 遍历转成学生对象
print("第"+str(i)+"行:", array[i])
if isinstance(array[i], str): # 判断是否为字符串
strArray = str(array[i]).split(",")
id = strArray[0]
name = strArray[1]
age = strArray[2]
score = strArray[3].replace("\n","") #去掉换行符
student = Student(id,name,age,score)#创建学生对象
array_temp.append(student)
del array #马上删除array
array = array_temp #将新的赋值给原来的
except:
print("文件不存在")
return
def main():
flag = 0
array = [] #定义list用于保存学生信息
while flag != 1:
meun()
step = input("请输入你的操作:")
step = int(step)
if step == 1:
addStu(array)
#print("学生信息打印:", array)
elif step == 2:
num = delStu(array)
if num == 0:
print("删除成功")
elif num == 1 or num == 2:
print("删除失败")
printStuInfo(array)
elif step == 3:
updateStu(array)
printStuInfo(array)
elif step == 4:
selectStu(array)
elif step == 5:
printStuInfo(array)
elif step == 6:
printStuInfo(array)
elif step == 7:
save_info(array)
elif step == 8:
read_info(array)
else:
print("输入指令错误,请重新输入!!")
print("退出系统成功")
main()