1.学生的信息有:姓名、学号、年龄、性别、出生日期、地址、电话、E-mail等。
2.功能:
·系统以菜单方式工作
·学生信息录入功能(学生信息,用文件保存)–>来自输入
·学生信息浏览功能–>输出
·查询、排序功能–>算法
按学号查询、按姓名查询、学生信息的删除与修改。
import os class Student(object): def _init_(self,sid,name,age=None,gender=None, birth=None,address=None,tel = None,mail= None): self.sid = sid self.name = name self.age = age self.gender = gender self.birth = birth self.address = address self.tel = tel self.mail = mail def _str_(self): return "%s: %s: %s: %s:"(8) %(self.sid,self.name,self.age, self.gender,self.birth,self.address,self.tel,self.mail) class StudentManage(object): Students = [] def _init_(self): filename = 'students.csv' if os.path.exsits(filename): with open(filename) as f: for line in f: into = line.split(':') Studentobj = Student(*into) self.Students.append(Studentobj) print("加载缓存的学生信息成功") else: print("无缓存的学生信息") def add(self): sid = input("学号:") name = input("姓名:") age = input("年龄:") gender = input("性别:(1-男 2-女)") birth = input("出生日期(年-月-日):") address = input("地址:") tel = input("联系方式:") mail = input("电子邮箱:") stu = Student(sid,name,age,gender,birth,address,tel,mail) self.Students.append(stu) print("录入学生[%s]信息成功"%(name)) def show(self): print("学生信息展示".center(50,'*')) for student in self.Students: print('_'*50) print(str(student)) def search(self,key,Value): if key in ['sid','name']: for student in self.Students: if getattr(student,key)==Value: print(str(student)) else:print("未找到符合条件的学生信息!") else: print("系统只支持根据【学号】和【姓名】查询!") def sort(self, key): # 根据指定的key值进行排序 sorted_students = sorted(self.students, key=lambda x: int(getattr(x, key)) if x else None) for student in sorted_students: print(student) def delete(self, key, value): if key in ['sid', 'name']: for student in self.students: if getattr(student, key) == value: self.students.remove(student) print("删除属性值[%s]为[%s]的学生信息" % (key, value)) else: print("未找到符合条件的学生信息!") else: print("当前系统只支持根据 [学号] 和 [姓名] 删除!") def modify(self, key, oldvalue, newvalue): if key in ['sid', 'name']: for student in self.students: if getattr(student, key) == oldvalue: setattr(student, key, newvalue) print("修改属性值[%s]为[%s]的学生信息为[%s]成功" % (key, oldvalue, newvalue)) else: print("未找到符合条件的学生信息!") else: print("当前系统只支持根据 [学号] 和 [姓名] 修改!") def save(self): filename = 'students.csv' f = open(filename, 'w') for student in self.students: f.write(str(student) + '\n') print("写入学生信息成功!") f.close() def main(self): manager = StudentManage() while True: menu = """ 学生信息管理系统 1. 学生信息录入 2. 学生信息浏览 3. 学生信息查询 4. 学生信息排序 5. 学生信息的删除 6. 学生信息的修改 7. 退出 请输入正确的选择: """ choice = input(menu) if choice == '1': manager.add() manager.save() elif choice == '2': manager.show() elif choice == '3': key = input("查询(sid-根据学号, name-根据姓名): ") value = input("查询值: ") manager.search(key, value) elif choice == '4': key = input("排序(sid-根据学号, age-根据年龄): ") manager.sort(key) elif choice == '5': key = input("删除(sid-根据学号, name-根据姓名): ") value = input("删除的属性值: ") manager.delete(key, value) elif choice == '6': key = input("修改(sid-根据学号, name-根据姓名): ") value1 = input("旧的属性值: ") value2 = input("新的属性值: ") manager.modify(key, value1, value2) elif choice == '7': exit(0) else: print("请输入正确的选择!") main()
`