运行 使用 python 2.7.14 的函数调用表达式时语法无效

invalid syntax when running a function call expression using python 2.7.14

不能运行空闲时的函数调用表达式python 2.7.14。下面的代码可能有什么问题?

fruit = ['apple', 'orange', 'grape', 'apple',
         'orange', 'grape', 'kiwi']  # init the array
#reports the frequency of every item in a list

def analyze_list(l):
    counts = {}
    for item in l:

        if item in counts:
            counts[item] = counts[item] + 1
            else:
                counts[item] = 1

                return counts

    #let analyze a list
    counts =  analyze_list(fruit)
    print counts

您的代码缩进不正确,与 if 语句对应的 else 或 elif 语句必须在同一缩进块中。

fruit = ['apple', 'orange', 'grape', 'apple', 'orange', 'grape', 'kiwi']  # init the array
#reports the frequency of every item in a list 
def analyze_list(l):
    counts = {}
    for item in l:

        if item in counts:
            counts[item] = counts[item] + 1
        else:
            counts[item] = 1

            return counts

    #let analyze a list
    counts =  analyze_list(fruit)
    print counts 

注意 if 语句与 else 语句的一致性。