1. Create a new class athlete, create two unique object instances Sarah James, they inherit the attributes of the athlete class.
2. Applications of classes and their objects: Senitize invariant, defining class athlete and two sub-methods __init__ and TOP3. Call through the Get_coach_data function
def senitize (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) 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 ([Senitize (t) to T in Self.times])) [0:3]) def Get_coach_ Data (filename): Try:with open (filename) as F:data=f.readline () User=data.strip (). Split (' , ') Userob=athlete (user.pop (0), User.pop (0), user) print (userob.name+ "s fastest times is:" + str (use ROB.TOP3 ())) except IOError as Ioerr:print (' File error ' +str (Ioerr)) return (None) get_coach_data (' Sara H2.txt ') get_coach_data (' James2.txt ') get_coach_data (' Mikey2.txt ') get_coach_data (' Julie2.txt ') ========== RESTART: c:/users/eric/documents/python/kelly/kelly2.py ==========sarAh Sweeney ' s fastest times are:[' 2.18 ', ' 2.21 ', ' 2.22 ']james Lee ' s fastest times are:[' 2.01 ', ' 2.16 ', ' 2.22 ']mikey McManus ' s fastest times are:[' 2.22 ', ' 2.31 ', ' 2.38 ']julie Jones ' s fastest times are:[' 2.11 ', ' 2.23 ', ' 2.59 '
3. Add 2 new methods to the athlete class and call the test
def senitize (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) 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 ([Senitize (t) to T in Self.times])) [0:3]) def Add_ti Me (Self,time_value): Self.times.append (Time_value) def add_times (self,list_of_times): Self.times.extend (l Ist_of_times) vera=athlete (' Veraname ') vera.add_time (' 1.31 ') print (' Object name is: ', vera.name) print (vera.name+ "' s TOP3 is: "+ str (VERA.TOP3 ())) vera.add_times ([' 2.12 ', ' 3.44 ', ' 3.33 ']) print (vera.name+" s TOP3 is: "+ str (VERA.TOP3 ()))
========== restart:c:/users/eric/documents/python/kelly/kelly3.py ==========object name Is:veranameveraname ' s TOP3 is: [' 1.31 ']veraname's top3 is: [' 1.31 ', ' 2.12 ', ' 3.33 ']>>>
The creation and application of Python classes and their objects