Page 186 -
P. 186
sanitzing function
Code Magnets Solution
You were to create a function called sanitize(), which takes
as input a string from each of the athlete’s lists. The function then
processes the string to replace any dashes or colons found with a
period and returns the sanitized string. Note: if the string already
contains a period, there’s no need to sanitize it.
You were to rearrange the code magnets at the bottom of the
previous page to provide the required functionality.
def sanitize(time_string):
if '-' in time_string:
Use the “in” splitter = '-'
operator to elif ':' in time_string:
check if the
string contains splitter = ':'
a dash or a
colon. else: Do nothing if the string does
return(time_string) NOT need to be sanitized.
(mins, secs) = time_string.split(splitter)
Split the string to extract the
return(mins + '.' + secs)
minutes and seconds parts.
Of course, on its own, the sanitize() function is not enough. You need
to iterate over each of your lists of data and use your new function to convert
each of the athlete’s times into the correct format.
Let’s put your new function to work right away.
150 Chapter 5