首页 > 解决方案 > Unndent 不匹配任何外部缩进级别

问题描述

尝试运行以下 python 代码,我收到缩进错误:

    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()

标签: pythonpython-3.xpython-2.7

解决方案


检查此缩进是否适合您

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()

推荐阅读