1 def sanitize(time_string): 2 if '-' in time_string: 3 splitter = '-' 4 elif ':' in time_string: 5 splitter = ':' 6 else : 7 return (time_string) 8 (mins,secs) = time_string.split(splitter) 9 return (mins + '.' +secs) 10 11 12 class Athlete: 13 def __init__(self,a_name,a_dob=None,a_times=[]): 14 self.name = a_name 15 self.dob = a_dob 16 self.times = a_times 17 18 19 20 def get_coach_data (filename): 21 try: 22 with open (filename) as f: 23 data =f.readline() 24 templ = data.strip().split(',') 25 return(Athlete(templ.pop(0),templ.pop(0),templ)) 26 except IOError as ioerr: 27 print('File Error' + str(ioerr)) 28 return(None) 29 30 def top3(self): 31 return(sorted(set([sanitize(t) for t in self.times]))[0:3]) 32 33 james = get_coach_data('james2.txt') 34 print(james.name) 35 print(james.times) 36 print(str(james.top3()))
Traceback (most recent call last):
File "C:/Lib/idlelib/12.py", line 36, in <module>
print(str(james.top3()))
AttributeError: 'Athlete' object has no attribute 'top3'
格式不对,top3()应该有缩进。。
格式不对吧。应该是有缩进的
写Python竟然没使用游标卡尺(你要把top3提到类Athlete中吧?)
是的。没注意到格式问题