Traceback (most recent call last):
File "C:Libidlelib13.py", line 40, in <module>
print (james.top3())
AttributeError: 'Athlete' object has no attribute 'top3'
1 class Athlete: 2 def __init__ (self,a_name,a_dob = None,a_times =[]): 3 self.name = a_name 4 self.dob = a_dob 5 self.times = a_times 6 7 def top(self): 8 return(sorted(set([sanitize(t) for t in self.times]))[0:3]) 9 10 11 def sanitize(time_string): 12 if '-' in time_string: 13 splitter = '-' 14 elif ':' in time_string: 15 splitter = ':' 16 else : 17 return (time_string) 18 (mins,secs) = time_string.split(splitter) 19 return (mins + '.' +secs) 20 21 def get_coach_data (filename): 22 try: 23 with open (filename) as f: 24 data =f.readline() 25 templ = data.strip().split(',') 26 return(Athlete(templ.pop(0),templ.pop(0),templ)) 27 except IOError as ioerr: 28 print('File Error' + str(ioerr)) 29 return(None) 30 31 def add_time(self,time_value): 32 self.times.append(time_value) 33 34 def add_times(self,list_of_times): 35 self.times.extend(list_of_tiems) 36 37 james = get_coach_data('james2.txt') 38 print (str(james.top()))
修改错误的方法 :
1 class Athlete: 2 def __init__ (self,a_name,a_dob = None,a_times =[]): 3 self.name = a_name 4 self.dob = a_dob 5 self.times = a_times 6 7 def top(self): 8 return(sorted(set([sanitize(t) for t in self.times]))[0:3])
def top3(self) 前面加Tab
格式!!!