为什么 python 给我一个语法错误?

问题描述 投票:0回答:1

我有教程中的代码:

#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新手。我感谢任何人的帮助。预先感谢。

python python-3.x
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
    应该是第 26 行的
    julie.name

© www.soinside.com 2019 - 2024. All rights reserved.