缩进不匹配任何外部缩进级别

时间:2019-04-17 13:49:01

标签: python python-3.x python-2.7

尝试运行以下python代码,出现缩进错误:

NameError: name 'BlahBlahTypoWhatever' is not defined

2 个答案:

答案 0 :(得分:1)

检查该缩进是否适合您

from urllib.request import urlopen

def fetch_words():
   with urlopen('http://sixty-north.com/c/t/txt') as story:
       story_words = []
       for line in story:
           line_words = line.decode('utf-8').split()
           for word in line_words:
                story_words.append(word)
   return story_words

def print_words(story_words):
   for word in story_words:
       print(word)

def main():
   words = fetch_words()
   print_words(words)

if __name__ == '__main__':
   main()

答案 1 :(得分:0)

在尝试返回函数外时,您需要缩进return story_words

编辑

还要在story_words顶部设置with open声明。