Page 236 -
P. 236
more methods
Let’s add two methods to your class. The first, called add_time(), appends a single
additional timing value to an athlete’s timing data. The second, add_times(), extends an
athlete’s timing data with one of more timing values supplied as a list.
Here’s your current class: you were to add the code to implement these two new methods.
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([sanitize(t) for t in self.times]))[0:3])
def add_time(self, time_value):
self.times.append(time_value) T ake the supplied argument and
append it to the existing list of
Don’t forget to timing values.
use “self”!!!
def add_times(self, list_of_times): T ake the list of supplied arguments
self.times.extend(list_of_times) and extend the existing list of timing
values with them.
While still holding on firmly to your pencil, you were to provide a
Create a new few lines of code to test your new functionality:
object instance vera = Athlete(‘Vera Vi’)
for Vera. vera.add_time(‘1.31’) Add a single timing value.
print(vera.top3()) This will display a list with only one value in it: 1.31.
vera.add_times([‘2.22’, “1-21”, ‘2:22’]) Add three more timing values.
print(vera.top3()) The top 3 timing scores are now: 1.21, 1.31 and 2.22.
200 Chapter 6