‘james,2006-11-11,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22’
存储以上的数据,如何定义运动员类,补全代码(4分)
class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
数据所在文件的路径为’work/james_new.txt’,使用运动员对象,补全代码(4分)
def get_coach_data(filename):
with open(filename) as f:
line = f.readline()
return line.strip().split(',')
#从文件中读取数据
#代码1
james_new=get_coach_data('work/james_new.txt')
#代码2
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times=james_new
#创建Athlete对象
james = Athlete(james_name,james_dob,james_times)
#代码3
#代码4
print('姓名:%s,生日:%s,最快的3次成绩:%s' %(james.name,james.dob,james.top3()))
姓名:james,生日:2006-11-11,最快的3次成绩:['2.01', '2.22', '2.34']
类属性,类方法,补全代码(4分)
class Athlete:
#运动员集训了,要买东西的同学要把地址改一下
#代码1
address = '拉萨拉萨拉萨————海南集训地'
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
#代码2
#代码3
james_new = get_coach_data('work/james_new.txt')
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times = james_new
james = Athlete(james_name,james_dob,james_times)
print(james.address)
print(Athlete.address)
拉萨拉萨拉萨————海南集训地
拉萨拉萨拉萨————海南集训地
将第3题中的实例变量name改为私有的属性,将sanitize改为私有方法,补全代码(4分)
class Athlete:
#运动员集训了,要买东西的同学要把地址改一下
#代码1
address = '拉萨拉萨拉萨————海南集训地'
def __init__(self,a_name,a_dob=None,a_times=[]):
self.__name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def __sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
#代码2
#代码3
james_new = get_coach_data('work/james_new.txt')
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times = james_new
james = Athlete(james_name,james_dob,james_times)
print(james.address)
print(Athlete.address)
拉萨拉萨拉萨————海南集训地
拉萨拉萨拉萨————海南集训地
数据内容james,2006-11-11,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,以分钟.秒的形式打印’2-34’后面的所有时间。
输出的结果为’2.34’, ‘3.21’, ‘2.34’, ‘2.45’, ‘3.01’, ‘2.01’, ‘2.01’, ‘3.10’, ‘2.22’,补全代码。(4分)
data = 'james,2006-11-11,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22'
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
# #代码2
# name = james.pop(0)
# dob = james.pop(0)
#代码3
data=data.split(',')
for time in data[2:]:
print(sanitize(time))
# print(times)
2.34
3.21
2.34
2.45
3.01
2.01
2.01
3.10
2.22
请点击此处查看本环境基本用法.
Please click here for more detailed instructions.