转眼间百度飞桨的零基础python课程马上就要结束了,所谓年前学python,年后来上号,通过本次课程可以对python有一个基础的认知和掌握,以下是大作业的个人代码,仅供参考。
【作业一】
#创建Student类
class Student:
def __init__(self,a_name,a_dob,a_age,a_gender,a_score=[]):
self.name = a_name
self.dob = a_dob
self.age = a_age
self.gender = a_gender
self.score = a_score
def top3(self):
return sorted([self.sanitize(t) for t in self.score])[-3:]#从小到大,最大三个为倒数三个
def sanitize(self,a_score):
if '-' in a_score:
scorestring = abs(int(a_score))
else:
scorestring = int(a_score)
return scorestring
def get_coach_data(filename):
with open(filename) as f:
line = f.readline()
return line.strip().split(',')
#读取数据
sun_data = get_coach_data('work/stu1.txt')
zhao_data = get_coach_data('work/stu2.txt')
wang_data = get_coach_data('work/stu3.txt')
li_data = get_coach_data('work/stu4.txt')
#创建对象
sun = Student(sun_data.pop(0),sun_data.pop(0),sun_data.pop(0),sun_data.pop(0),sun_data)
zhao = Student(zhao_data.pop(0),zhao_data.pop(0),zhao_data.pop(0),zhao_data.pop(0),zhao_data)
wang = Student(wang_data.pop(0),wang_data.pop(0),wang_data.pop(0),wang_data.pop(0),wang_data)
li = Student(li_data.pop(0),li_data.pop(0),li_data.pop(0),li_data.pop(0),li_data)
#创建stu_list列表
stu_list = []
stu_list.append(sun)
stu_list.append(zhao)
stu_list.append(wang)
stu_list.append(li)
#输出信息
for stu in stu_list:
print('姓名:%s,生日:%s,年龄:%s,性别:%s,分数:%s' %(stu.name,stu.dob,stu.age,stu.gender,stu.top3()))
【作业二】
#Spostdent继承Student
class Spostdent(Student):
def __init__(self,a_name,a_dob,a_age,a_gender,a_spe,a_score):
#调用父类的构造方法,传递的参数为a_name,a_dob,a_age,a_gender,a_score
Student.__init__(self,a_name,a_dob,a_age,a_gender,a_score)
self.spe = a_spe
def top3(self):
return sorted([self.sanitize(t) for t in self.score])[:3]
#Artstudent继承Student
class Artstudent(Student):
def __init__(self,a_name,a_dob,a_age,a_gender,a_spe,a_score,):
#调用父类的构造方法,传递的参数为a_name,a_dob,a_age,a_gender,a_score
Student.__init__(self,a_name,a_dob,a_age,a_gender,a_score)
self.spe = a_spe
#读取信息
fifth_data = get_coach_data('work/stu5.txt')
sixth_data = get_coach_data('work/stu6.txt')
#取出信息
fifth_name = fifth_data.pop(0)
fifth_dob = fifth_data.pop(0)
fifth_age = fifth_data.pop(0)
fifth_gender = fifth_data.pop(0)
fifth_spe =fifth_data.pop(0)
fifth_score = fifth_data
sixth_name = sixth_data.pop(0)
sixth_dob = sixth_data.pop(0)
sixth_age = sixth_data.pop(0)
sixth_gender = sixth_data.pop(0)
sixth_spe = sixth_data.pop(0)
sixth_score = sixth_data
#创建对象
fifth = Spostdent(fifth_name,fifth_dob,fifth_age,fifth_gender,fifth_spe,fifth_score)
sixth = Artstudent(sixth_name,sixth_dob,sixth_age,sixth_gender,sixth_spe,sixth_score)
list2 = []
list2.append(fifth)
list2.append(sixth)
for stu in list2:
print('姓名:%s,生日:%s,年龄:%s,性别:%s,分数:%s,特征分:%s' %(stu.name,stu.dob,stu.age,stu.gender,stu.top3(),stu.spe))