zoukankan      html  css  js  c++  java
  • 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的代码对齐要求比较严格,如果书写有误,会自动检查出,并提示此错误。

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

  • 相关阅读:
    AE二次开发,解决子窗体使用父窗体的AxControl控件
    ArcEngine二次开发中运行出现There is no Spatial Analyst license currently available or enabled.
    Js网站开发学习第一天
    Winform开发1
    MySql安装
    Windows ping加时间戳
    XML特性总结
    linux手册中函数名后小括号中数字的含义
    TCP通信
    swap交换分区概念
  • 原文地址:https://www.cnblogs.com/kevin-hou1991/p/13681866.html
Copyright © 2011-2022 走看看