0

我有一个教程中的这段代码:

#File called test
1 def sanitize(time_string):
2         if '-' in time_string:
3                 splitter = '-'
4         elif ':' in time_string:
5                 splitter = ':'
6         else:
7                 return(time_string)
8         (mins, secs) = time_string.split(splitter)
9         return(mins + '.' + secs)
10 
11 
12         
13 def get_coach_data(filename):
14         with open(filename) as f:
15                 data = f.readline()
16         temp1 = data.strip().split(',')
17         return(Athlete(temp1.pop(0), temp1.pop(0), temp1)
18 
19 
20 james = get_coach_data('james2.txt')
21 julie = get_coach_data('julie2.txt')
22 mikey = get_coach_data('mikey2.txt')
23 sarah = get_coach_data('sarah2.txt')
24 
25 print(james.name+"'s fastest times are: " + str(james.top3()))
26 print(juliename+"'s fastest times are: " + str(julie.top3())) 
27 print(mikey.name+"'s fastest times are: " + str(mikey.top3()))
28 print(sarah.name+"'s fastest times are: " + str(sarah.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 top3(self):
 8                 return(sorted(set([sanitize(t) for t in self.times]))[0:3])

错误是:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 20
james = get_coach_data('james2.txt')

但是这个错误没有任何意义。我是 python 新手。我感谢任何人的帮助。提前致谢。

4

1 回答 1

2

我可以看到的错误是:

  1. return(Athlete(temp1.pop(0), temp1.pop(0), temp1)
    

    get_coach_data应该只是

    return Athlete(temp1.pop(0), temp1.pop(0), temp1)
    

    在第 17 行

  2. juliename应该julie.name在第 26 行

于 2012-04-28T01:28:17.387 回答