IndentationError: unindent does not match any outer indentation level

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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的代码对齐要求比较严格,如果书写有误,会自动检查出,并提示此错误。

 

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

posted @   JRS077  阅读(242)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示