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 (Retired) Dictionaries Word Count

don't understand the error

I have word_count() in my code. I don't understand why the error says 'where's word_count()".

word_count.py
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count(my_str):
  my_str = my_str.lower()
  my_list = my_str.split()
  for word in my_list:
    if word in my_dict:
      my_dict[word] += 1
    else:
      my_dict[word] = 1
  return my_dict

1 Answer

Hi Homa

The reason you are getting that error is because there is an error in the script. my_dict is not declared as a dictionary.Try the code below and notice line 4.

def word_count(my_str):
  my_str = my_str.lower()
  my_list = my_str.split()
  my_dict={}
  for word in my_list:
    if word in my_dict:
      my_dict[word] += 1
    else:
      my_dict[word] = 1
  return my_dict

I just defined my_dicts as you suggested (which i did forget) but it didn't fix the problem. It still says "where's is word_count()"

Hi Homa

I just copy pasted the code from above in the code challenge and it passed.

Hi Andres

I had my_dicts instead of my_dict. It worked thank you :)