IndentationError: unindent does not match any outer indentation level
# Author kevin_hou
def sanitize(time_string): #格式化时间
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins, secs) = time_string.split(splitter)
return (mins + '.' + secs)
class AthleteList(list): #定义类
def __init__(self, a_name, a_dob=None, a_times=[]):
list.__init__([])
self.name = a_name
self.dob = a_dob
self.extend(a_times)
def top3(self): #定义排在前3的函数
return (sorted(set([sanitize(t) for t in self]))[0:3])
def get_coach_data(filename): #打开文件获取数据
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return (AthleteList(temp1.pop(0), temp1.pop(0), temp1))
except IOError as ioerr:
print('File error:' + str(ioerr))
return(None)
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
print(james.name +" 's fastest times are:" + str(james.top3()))
#James Lee 's fastest times are:[' 2.34', '2.01', '2.22']
print(sarah.name +" 's fastest times are: " + str(sarah.top3()))
#Sarah Sweeney 's fastest times are: ['2.18', '2.25', '2.39']

出现这个错误的原因是115,116行代码起始位置存在空格,只要将相应的空格去除就OK了。这里强调一下,python的代码对齐要求比较严格,如果书写有误,会自动检查出,并提示此错误。

去掉空格运行正常,输出正常。有时候不是代码逻辑输入错误,而是格式或对齐方式输入错误导致代码执行出错。

浙公网安备 33010602011771号