├── MVC.py ├── README.md ├── file.py ├── main.py ├── menu.py ├── sorted.py └── stu_info.py /MVC.py: -------------------------------------------------------------------------------- 1 | """ 2 | 学生管理 3 | """ 4 | #存储学生信息name age score id 5 | class StudentModel: 6 | def __init__(self,name="",age=0,score=0,id=1): 7 | self.name=name 8 | self.age=age 9 | self.score=score 10 | self.id=id 11 | 12 | 13 | class StudentController: 14 | #公用的变量--类变量 15 | stu_id=1 16 | 17 | def __init__(self): 18 | self.__stu_list=[] 19 | 20 | @classmethod 21 | def __generate_id(cls,stu): 22 | stu.id=cls.stu_id 23 | #每插入一个学生增加1 24 | cls.stu_id+=1 25 | 26 | def insert_student(self): 27 | name=input('name:') 28 | age=int(input('age:')) 29 | score=int(input('score:')) 30 | id=int(input('id:')) 31 | #得到一个学生 32 | stu=StudentModel(name=name,age=age,score=score,id=id) 33 | StudentController.__generate_id(stu) 34 | #添加学生 35 | self.__stu_list.append(stu) 36 | print(self.__stu_list) 37 | self.show_student() 38 | 39 | def show_student(self): 40 | for item in self.__stu_list: 41 | print('ID:%d-name:%s-age:%d-score:%d'%(item.id,item.name,item.age,item.score)) 42 | 43 | def delete_student(self): 44 | name = input('delete name:') 45 | for item in self.__stu_list: 46 | if item.name==name: 47 | self.__stu_list.remove(item) 48 | print('delete success') 49 | break 50 | self.show_student() 51 | 52 | def change_student(self): 53 | #获取索引 lst.index('xxx') 54 | name=input('change name:') 55 | for item in self.__stu_list: 56 | if item.name==name: 57 | score=int(input('change score:')) 58 | item.score=score 59 | 60 | self.show_student() 61 | 62 | def sort_student(self): 63 | for i in range(len(self.__stu_list)-1): 64 | for j in range(len(self.__stu_list)-i-1): 65 | if self.__stu_list[j].score>self.__stu_list[j+1].score: 66 | self.__stu_list[j],self.__stu_list[j+1]=self.__stu_list[j+1],self.__stu_list[j] 67 | 68 | self.show_student() 69 | 70 | class StudentView: 71 | def __init__(self): 72 | self.controller=StudentController() 73 | 74 | # 私有方法 75 | def __display_menu(self): 76 | print(""" 77 | 1) 添加学生信息 78 | 2) 显示学生信息 79 | 3) 删除学生信息 80 | 4) 修改学生成绩 81 | 5) 学生成绩升序排序 82 | """) 83 | 84 | def __select_menu(self): 85 | cmd=input('请输入选项:') 86 | if cmd == '1': 87 | self.controller.insert_student() 88 | 89 | elif cmd == '2': 90 | self.controller.show_student() 91 | elif cmd == '3': 92 | self.controller.delete_student() 93 | elif cmd == '4': 94 | self.controller.change_student() 95 | elif cmd == '5': 96 | self.controller.sort_student() 97 | else: 98 | print('请输入正确选项') 99 | 100 | 101 | def main(self): 102 | while True: 103 | self.__display_menu() 104 | #选择功能 105 | self.__select_menu() 106 | 107 | 108 | 109 | stu=StudentView() 110 | stu.main() 111 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 学生信息管理系统python代码实现功能: 2 | 3 | +--------------------------------+ 4 | 5 | | 1) 添加学生信息 | 6 | 7 | | 2) 查看所有学生信息 | 8 | 9 | | 3) 修改学生的成绩 | 10 | 11 | | 4) 删除学生信息 | 12 | 13 | | 5) 按成绩从高至低打印学生信息 | 14 | 15 | | 6) 按成绩从低至高打印学生信息 | 16 | 17 | | 7) 按年龄从大到小打印学生信息 | 18 | 19 | | 8) 按年龄从小到大打印学生信息 | 20 | 21 | | 9) 保存信息到文件(si.txt) | 22 | 23 | | 10)从文件中读取数据(si.txt) | 24 | 25 | | q) 退出 | 26 | 27 | +--------------------------------+ 28 | 29 | ------------------------------------------------------------------------- 30 | 31 | 定义模块如下: 32 | file.py main.py menu.py sorted.py stu_info.py 33 | 程序丢同一个文件夹内运行就好了 34 | 35 | 注:要和数据库连接的话,在python3中使用pymysql和mysql数据库相联系从而对数据进行存储,或者以字典的方式存储数据来替代列表的存储方式会更好一点 36 | -------------------------------------------------------------------------------- /file.py: -------------------------------------------------------------------------------- 1 | def save_file(all_info): 2 | f=open('si.txt','w') 3 | for i in all_info: 4 | f.write('|%s|%s|%s|\n' % (i['name'].center(20),str(i['age']).center(6),str(i['score']).center(6))) 5 | f.close() 6 | 7 | def read_file(): 8 | f=open('si.txt','r') 9 | while True: 10 | s=f.readline() 11 | if s=='': 12 | break 13 | print(s) 14 | f.close() 15 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from menu import * 2 | from stu_info import * 3 | from sorted import * 4 | from file import * 5 | 6 | all_info=[] 7 | while True: 8 | show_menu() 9 | num=input('请选择:') 10 | if num=='1': 11 | all_info=add_stu_info(all_info) 12 | elif num=='2': 13 | show_stu_info(all_info) 14 | elif num=='3': 15 | chang_stu_info(all_info) 16 | elif num=='4': 17 | del_stu_info(all_info) 18 | elif num=='5': 19 | score_high2low(all_info) 20 | elif num=='6': 21 | score_low2high(all_info) 22 | elif num=='7': 23 | age_high2low(all_info) 24 | elif num=='8': 25 | age_low2high(all_info) 26 | elif num=='9': 27 | save_file(all_info) 28 | elif num=='10': 29 | read_file() 30 | elif num=='q': 31 | break 32 | -------------------------------------------------------------------------------- /menu.py: -------------------------------------------------------------------------------- 1 | def show_menu(): 2 | print('+--------------------------------+') 3 | print('| 1) 添加学生信息 |') 4 | print('| 2) 查看所有学生信息 |') 5 | print('| 3) 修改学生的成绩 |') 6 | print('| 4) 删除学生信息 |') 7 | print('| 5) 按成绩从高至低打印学生信息 |') 8 | print('| 6) 按成绩从低至高打印学生信息 |') 9 | print('| 7) 按年龄从大到小打印学生信息 |') 10 | print('| 8) 按年龄从小到大打印学生信息 |') 11 | print('| 9) 保存信息到文件(si.txt) |') 12 | print('| 10)从文件中读取数据(si.txt) |') 13 | print('| q) 退出 |') 14 | print('+--------------------------------+') 15 | -------------------------------------------------------------------------------- /sorted.py: -------------------------------------------------------------------------------- 1 | def score_high2low(all_info): 2 | res=sorted(all_info,key=lambda all_info:all_info['score'],reverse=True) 3 | print('按成绩从高至低打印学生信息:') 4 | for n in res: 5 | print('|%s|%s|%s|' % (n['name'].center(20),str(n['age']).center(6),str(n['score']).center(6))) 6 | 7 | def score_low2high(all_info): 8 | res=sorted(all_info,key=lambda all_info:all_info['score']) 9 | print('按成绩从低至高打印学生信息:') 10 | for n in res: 11 | print('|%s|%s|%s|' % (n['name'].center(20),str(n['age']).center(6),str(n['score']).center(6))) 12 | 13 | def age_high2low(all_info): 14 | res=sorted(all_info,key=lambda all_info:all_info['age'],reverse=True) 15 | print('按年龄从高至低打印学生信息:') 16 | for n in res: 17 | print('|%s|%s|%s|' % (n['name'].center(20),str(n['age']).center(6),str(n['score']).center(6))) 18 | 19 | def age_low2high(all_info): 20 | res=sorted(all_info,key=lambda all_info:all_info['age']) 21 | print('按年龄从低至高打印学生信息:') 22 | for n in res: 23 | print('|%s|%s|%s|' % (n['name'].center(20),str(n['age']).center(6),str(n['score']).center(6))) 24 | -------------------------------------------------------------------------------- /stu_info.py: -------------------------------------------------------------------------------- 1 | def add_stu_info(all_info): 2 | while True: 3 | name=input('请输入学生姓名:') 4 | if not name: 5 | break 6 | age=int(input('请输入学生年龄:')) 7 | score=int(input('请输入学生成绩:')) 8 | all_info+=[{'name':name,'age':age,'score':score}] 9 | return all_info 10 | 11 | def show_stu_info(all_info): 12 | for i in all_info: 13 | #print(i) 14 | print('|%s|%s|%s|' % (i['name'].center(20),str(i['age']).center(6),str(i['score']).center(6))) 15 | 16 | def chang_stu_info(all_info): 17 | flag=0 18 | name=input('请输入要修改信息的学生姓名:') 19 | for j in all_info: 20 | if j['name']==name: 21 | flag=1 22 | print('****系统找到该学生的信息****') 23 | age=int(input('请输入学生年龄:')) 24 | score=int(input('请输入学生成绩:')) 25 | j['age']=age 26 | j['score']=score 27 | print('修改后的学生信息是:','|%s|%s|%s|' % (j['name'].center(20),str(j['age']).center(6),str(j['score']).center(6))) 28 | 29 | if flag==0: 30 | print('系统没有',name,'的学生信息,无法修改') 31 | 32 | def del_stu_info(all_info): 33 | flag=0 34 | count=0 35 | name=input('请输入学生姓名:') 36 | for k in all_info: 37 | if k['name']==name: 38 | del all_info[count] 39 | flag=1 40 | print('学生',name,'的信息已经删除') 41 | count+=1 42 | if flag==0: 43 | print('系统没有',name,'的学生信息,不需要删除') 44 | --------------------------------------------------------------------------------