面向对象作业

第1题

‘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)

第2题

数据所在文件的路径为’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']

第3题

类属性,类方法,补全代码(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)

拉萨拉萨拉萨————海南集训地
拉萨拉萨拉萨————海南集训地

第4题

将第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)

拉萨拉萨拉萨————海南集训地
拉萨拉萨拉萨————海南集训地

第5题

数据内容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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值