如何修复此错误:“NameError:未定义全局名称'filtered_text'”;在下面的代码?

时间:2017-04-16 18:25:57

标签: python python-2.7

numberOfSectionsInTableView

1 个答案:

答案 0 :(得分:0)

在函数dict_count中,您从未创建过filtered_text变量,然后您想要使用它。

您必须先创建变量,然后才能使用:

filtered_text = filter_file(files)

完整代码:

def dict_count(files):
    """ for loop to return dict with word count and length keys """
    lengths = {}
    filtered_text = filter_file(files)
    for word in filtered_text:  # And this also
        length = len(word)
        if length not in lengths:
            lengths[length] = 0
        lengths[length] += 1
    for length, counter in lengths.item():
        return "Words of length %d: %d" % (length, counter)