Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Word Count

When running this code in another IDE I am getting the correct results. Although here It is declared as incorrect.

Its telling me to make sure I lowercase the string, although that is the very first thing I do aside from splitting the string up into a list to iterate through each word.

wordcount.py
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.

def word_count(string):
  list_of_words = string.lower().split()

  dict = {}

  counter = 1

  for word in list_of_words:
    for key in dict.keys():
      if (word == key):
        counter += 1
    dict[word] = counter
    counter = 1


  return dict

1 Answer

You are only indenting your function with 2 spaces. Remember to indent with 4 and it should work.