尝试运行以下python代码,出现缩进错误:
NameError: name 'BlahBlahTypoWhatever' is not defined
答案 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
声明。