Python 学生管理

原文链接: Python 学生管理

python3 练手 

1#coding=utf-8 2#学生类 3class Stu: 4 #三个字段id字符串,name字符串,score小数 5 def __init__(self, id, name, score): 6 self.id = id 7 self.name = name 8 self.score = score 9 #将类作为字符串输出 10 def __str__(self): 11 return self.id + '\t' + self.name + '\t' + str(self.score) 12 13#管理学生的类 14class StuMgr: 15 #由给定路径初始化stus列表 16 def __init__(self, path): 17 self.path = path 18 self.stus = [] 19 with open(self.path, 'r+', encoding='utf-8') as f: 20 n = int(f.readline()) 21 for i in range(n): 22 s = f.readline().split() # 默认空格分割 23 stu = Stu(str(s[0]), str(s[1]), float(s[2])) 24 self.stus.append(stu) 25 #由给定的id查找具体学生,不存在返回None 26 def findById(self, id): 27 for s in self.stus: 28 if s.id == id: 29 return s 30 return None 31 #由给定的id删除具体学生 32 def delById(self, id): 33 if self.findById(id) != None: 34 for s in range(len(self.stus)): 35 if self.stus[s].id == id: 36 self.stus.pop(s) 37 return True 38 else: 39 return False 40 41 def add(self, stu): 42 if self.findById(stu.id) != None: 43 return False 44 else: 45 self.stus.append(stu) 46 return True 47 #按照id排序 48 def sortById(self): 49 self.stus.sort(key=lambda Stu: Stu.id) 50 51 #保存到文件,编码utf--852 def saveToFile(self): 53 with open(self.path, 'w+', encoding='utf-8') as f: 54 f.write(str(len(self.stus))) 55 f.write('\n') 56 for s in self.stus: 57 f.write(str(s) + '\n') 58 59 def show(self): 60 print('共 %d 名学生' % (len(self.stus))) 61 for s in self.stus: 62 print(s) 63 #用于绘制界面,提供操作 64 def start(self): 65 while True: 66 try: 67 print("1,显示\t2,添加\t3,删除\t4,查找\t5,排序\t6,保存\t -by ahao") 68 opt = input(); 69 if opt == '1': 70 self.show() 71 elif opt == '2': 72 print('请输入要添加的学生信息,空格隔开\n学号\t\t姓名\t成绩') 73 ss = input().split(' ') 74 stu = Stu(str(ss[0]), str(ss[1]), float(ss[2])) 75 self.add(stu); 76 elif opt == '3': 77 print('请输入需要删除的学生id') 78 id = input() 79 if self.findById(id) != None: 80 self.delById(id) 81 print('删除成功') 82 else: 83 print('删除失败,该学号不存在') 84 elif opt == '4': 85 print('请输入查找的学生学号') 86 id = input() 87 if self.findById(id) != None: 88 s = self.findById(id) 89 print(s) 90 else: 91 print('该学生不存在') 92 elif opt == '5': 93 self.sortById() 94 print('排序成功') 95 elif opt == '6': 96 self.saveToFile() 97 print('保存成功') 98 else: 99 print('请输入有效命令') 100 except BaseException: 101 print('非法数据,请检查输入是否合法') 102 103#初始化文件路径 104stuMgr = StuMgr('stu.txt') 105stuMgr.start();

stu. 

16 22150500094 陈文梁 85.79 32160500079 崔致琪 83.44 42160500080 侯玥林 86.53 52160500081 刘佳雯 92.33 62160500082 石可心 86.72 72160500111 yyh 123.0
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''