A
ApathyBear
I have two questions that come along with the following code:
----------------------------------------------------------------------
from __future__ import print_function
def sanitize(time):
if '-' in time:
splitter = '-'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
elif ':' in time:
splitter = ':'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
else:
return time
#start class
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])
#end class
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
except IOError:
print ('File error')
return (None)
james = get_coach_data('james2.txt')
print (james.name + "'s fastest times are: " + str(james.top3()))
----------------------------------------------------------------------
This is the original text file data I am working with called james2.txt located on my desktop:
James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
----------------------------------------------------------------------
1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.
2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>
What syntax is invalid here?
----------------------------------------------------------------------
from __future__ import print_function
def sanitize(time):
if '-' in time:
splitter = '-'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
elif ':' in time:
splitter = ':'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
else:
return time
#start class
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])
#end class
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
except IOError:
print ('File error')
return (None)
james = get_coach_data('james2.txt')
print (james.name + "'s fastest times are: " + str(james.top3()))
----------------------------------------------------------------------
This is the original text file data I am working with called james2.txt located on my desktop:
James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
----------------------------------------------------------------------
1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.
2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>
What syntax is invalid here?