目录
作业1:
知识点1:eval使用
知识点1:类对象数组定义
知识点2:文件读取
知识点3:内容解包
知识点4:列表排序
作业2:
知识点1:类继承后 初始化管理
知识点2:文件的筛选
作业1:
数据如下:
stu1.txt 孙同学,2020-5-21,20,'男',77,56,77,76,92,58,-91,84,69,-91
stu2.txt 赵同学,2020-11-3,24,'女',65,68,72,95,-81,71,86,91,57,91
stu3.txt 王同学,2021-8-7,25,'男',87,78,90,-76,88,47,100,65,69,100
stu4.txt 李同学,2021-8-10,29,'男',92,54,85,71,-91,68,77,68,95,95
以上四个txt文档在work路径下可以找到。
定义Student类,包括name、dob、age、gender和score属性,包括top3方法用来返回学生的最大的3个成绩(可重复)、sanitize方法用来将负的分数变为正的分数,负的分数可能是输入错误。声明stu_list对象组数用于存储所有的学生对象。最后输出所有的学生信息包括姓名、生日、年龄、性别、最高的3个分数。
第一题的输出结果如下,供参考:
初始思路:
class Student:def __init__(self, name, dob, age, gender, score):self.name = nameself.dob = dobself.age = ageself.gender = genderself.score = scoredef top3(self):self.sanitize()self.score = sorted(self.score)self.score=self.score[-3:]def sanitize(self):for i in range(len(self.score)):if self.score[i]<0:self.score[i]=abs(self.score[i])def print_stu(self):self.top3()print('姓名:', self.name, '生日:', self.dob,'年龄:', self.age, '性别:', self.gender, '分数:', self.score)s1 = Student('孙同学','2020-5-21','20','男',[77,56,77,76,92,58,-91,84,69,-91])
s2 = Student('赵同学','2020-11-3','24','女',[65,68,72,95,-81,71,86,91,57,91])
s3 = Student('王同学','2021-8-7','25','男',[87,78,90,-76,88,47,100,65,69,100])
s4 = Student('李同学','2021-8-10','29','男',[92,54,85,71,-91,68,77,68,95,95])
s5 = Student('特长同学','2020-10-5','20','男',[180,77,56,77,76,92,58,-91,84,69,-91])
s6 = Student('特长同学','2020-10-6','20','女',[230,77,56,77,76,92,58,-91,84,69,-91])text1 = '孙同学', '2020-5-21','20','男',[77,56,77,76,92,58,-91,84,69,-91]
text2 = '赵同学', '2020-11-3','24','女',[65,68,72,95,-81,71,86,91,57,91]
text3 = '王同学','2021-8-7','25','男',[87,78,90,-76,88,47,100,65,69,100]
text4 = '李同学','2021-8-10','29','男',[92,54,85,71,-91,68,77,68,95,95]
text5 = '特长同学','2020-10-5','20','男',[180,77,56,77,76,92,58,-91,84,69,-91]
text6 = '特长同学','2020-10-6','20','女',[230,77,56,77,76,92,58,-91,84,69,-91]
print(type(text1))
# 赋值办法1
stu_lst = [Student for i in range(6)]
for i in range(len(stu_lst)):stu_lst[i] = Student(*eval('text'+str(i+1)))stu_lst[i].print_stu()# 赋值办法2
print('----------------------------')
for i in range(6):i = eval('s'+str(i+1))i.print_stu()
知识点1:eval使用
后续改进:
'''
定义Student类,包括name、dob、age、gender和score属性,
包括top3方法用来返回学生的最大的3个成绩(可重复)、
sanitize方法用来将负的分数变为正的分数,负的分数可能是输入错误。
声明stu_list对象组数用于存储所有的学生对象。
最后输出所有的学生信息包括姓名、生日、年龄、性别、最高的3个分数。
'''
import osclass Student:def __init__(self, name, dob, age, gender, score):self.name = nameself.dob = dobself.age = ageself.gender = genderself.score = scoredef top3(self):self.sanitize()self.score = sorted(self.score)self.score=self.score[-3:]def sanitize(self):self.score = self.score.split(',')self.score =[int(i) for i in self.score]for i in range(len(self.score)):if self.score[i]<0:self.score[i]=abs(self.score[i])def print_stu(self):self.top3()print("姓名:"+self.name,end=' ')print("生日:"+self.dob,end=' ')print('年龄:'+self.age,end=' ')print('性别:'+self.gender,end=' ')print('分数:'+str(self.score))path = './aistudio/work'
listidr = os.listdir(path)
info_lst = []
num=0
for name in listidr:num = num+1if os.path.isfile(path+'/'+name):temp = name.split('.')(filename, filetype) = (temp.pop(0), temp.pop(0))if filetype == 'txt':with open(path+'/'+name,'r', encoding='UTF-8') as f:text =f.read()temp = tuple(text.split(',',4))info_lst.append(temp)stu_lst = [Student for i in range(num)]
for i in range(len(stu_lst)):stu_lst[i] = Student(*info_lst[i])stu_lst[i].print_stu()
知识点1:类对象数组定义
知识点2:文件读取
知识点3:内容解包
知识点4:列表排序
作业2:
数据格式如下:
stu5.txt 特长同学,2020-10-5,20,'男',180,87,98,77,76,92,58,-76,84,69,-47
stu6.txt 特长同学,2020-10-6,20,'女',230,76,48,82,88,92,58,-91,84,69,-68
以上两个txt文档在work路径下可以找到。
定义Spostudent、Artstudent为Student的子类,在子类的属性里面新增了spe为特长分数。Spostudent包括的top3方法返回的是最低的3个得分(可重复),Artstudent包括top3方法返回的是最高的3个得分(可重复),最后使用多态的方式输出2个特长同学的姓名、生日、年龄、性别、分数、特长分。
第二题的输出结果如下,供参考:
import osclass Student:def __init__(self, name, dob, age, gender, score):self.name = nameself.dob = dobself.age = ageself.gender = genderself.score = scoredef sanitize(self):self.score = self.score.split(',')self.score = [int(i) for i in self.score]for i in range(len(self.score)):if self.score[i]<0:self.score[i]=abs(self.score[i])class Spostudent(Student):def __init__(self, name, dob, age, gender, score,spe):# super(Spostudent,self).__init__(spe)Student.__init__(self,name, dob, age, gender, score)self.spe = spedef top3(self):self.sanitize()self.score = sorted(self.score)self.score = self.score[0:3]def print_stu(self):self.top3()print("姓名:" + self.name, end=' ')print("生日:" + self.dob, end=' ')print('年龄:' + self.age, end=' ')print('性别:' + self.gender, end=' ')print('分数:' + str(self.score),end='')print('特长分:'+self.spe)class Artstudent(Student):def __init__(self, name, dob, age, gender, score,spe):Student.__init__(self, name, dob, age, gender, score)self.spe = spedef top3(self):self.sanitize()self.score = sorted(self.score)self.score = self.score[-3:]def print_stu(self):self.top3()print("姓名:" + self.name, end=' ')print("生日:" + self.dob, end=' ')print('年龄:' + self.age, end=' ')print('性别:' + self.gender, end=' ')print('分数:' + str(self.score), end=' ')print('特长分:' + self.spe)path = './aistudio/work'
listidr = os.listdir(path)
info_lst = []
num=0
for name in listidr:num = num+1if os.path.isfile(path+'/'+name):temp = name.split('.')(filename, filetype) = (temp.pop(0), temp.pop(0))if filetype == 'txt':with open(path+'/'+name,'r', encoding='UTF-8') as f:text =f.read()temp = list(text.split(',',5))temp[4], temp[5] = temp[5], temp[4]if "特长" in temp[0]:info_lst.append(temp)stu_1 = Spostudent(*info_lst[0])
stu_1.print_stu()stu_2 = Artstudent(*info_lst[1])
stu_2.print_stu()