-1

Attempting to run the following python code, I'm getting the Indentation error :

    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()
cdarke
  • 42,728
  • 8
  • 80
  • 84
Ian Falcon
  • 11
  • 2
  • The dupe handles all things: tabs vs spaces, wrong indentation, etc. The exact error message tells you more about your errors as well - and your posted code here would give you `IndentationError: unexpected indent` and not the error you get in your IDE. – Patrick Artner Apr 17 '19 at 15:30

2 Answers2

1

Check if this indentation works for you

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()
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

You need to indent the return story_words, as you are trying to return out of a function.

EDIT

Also set the story_words, declaration on top of the with open.

Dylan Logan
  • 395
  • 7
  • 18